目标
- 接收HTTP提交
- 返回json格式的结果
方案
用Maven管理SpringBoot项目. 例如:用NetBeans创建一个Maven类别的Java应用程序.
提示: Maven下载相关依赖的速度相当的慢. 可以通过建立本地的Maven库加速, 但是实现此技术比较复杂.
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.subook</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
模型
Book.java
package model;
public class Book {
private String isbn;
private String name;
public Book() {
}
public Book(String isbn, String name) {
this.isbn = isbn;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
}
Customer.java
package model;
public class Customer {
private String name;
private int age;
private Book[] books;
public Customer() {
}
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
}
控制器
RestServiceApplication1.java
package cn.com.subook.mavenproject1;
import model.Book;
import model.Customer;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class RestServiceApplication1 {
public static void main(String[] args) {
new SpringApplicationBuilder(RestServiceApplication1.class).web(WebApplicationType.SERVLET).run(args);
}
@RequestMapping(value = "/hello")
public ResponseEntity<String> hello(String name, int age) {
System.out.println(name + age * 2);
String message = "<h1>Hello,World!</h1>" + name + " age is " + age;
return new ResponseEntity<>(message, HttpStatus.OK);
}
@RequestMapping(value = "/hello1")
public ResponseEntity<Customer> hello1(String name, int age) {
Customer customer = new Customer(name + " hello1", age);
Book[] books = new Book[2];
for (int i = 0; i < books.length; i++) {
books[i] = new Book("isbn000000" + i, "bookname" + i);
}
customer.setBooks(books);
return new ResponseEntity<>(customer, HttpStatus.OK);
}
@RequestMapping(value = "/hello2")
public ResponseEntity<Customer> hello2(Customer customer/*String name, int age*/) {
//Customer customer = new Customer(name, age)
customer.setName(customer.getName() + " hello2");
Book[] books = new Book[2];
for (int i = 0; i < books.length; i++) {
books[i] = new Book("isbn000000" + i, "bookname" + i);
}
customer.setBooks(books);
return new ResponseEntity<>(customer, HttpStatus.OK);
}
}
编译打包
执行maven的目标package
运行
java -jar mavenproject1-1.0-SNAPSHOT.jar