配置
导入架包
<?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>com.fdk</groupId>
<artifactId>demo111301</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
resources目录创建appllication.properties文件
server.port=8080
创建文件ApplicationStart.java(名字随意)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationStart {
public static void main(String[] args) {
SpringApplication.run(ApplicationStart.class);
}
}
测试
创建Person.java
public class Person {
private String name;
private int age;
private Float height;
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 Float getHeight() {
return height;
}
public void setHeight(Float height) {
this.height = height;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
在ApplicationStart.java所在目录的子目录创建
UserController.java
import java.util.ArrayList;
import java.util.List;
@RestController //restful的控制器
@RequestMapping("/user")
public class UserController {
//输出数据到前端
@RequestMapping("/hello")
public String hello(){
System.out.println("你好丑,还责怪");
return "长得丑,想的美";
}
//传输对象类型到前端,json
@RequestMapping("/person")
public Person showperson(){
Person p=new Person();
p.setName("fdk");
p.setHeight(1.75f);
p.setAge(23);
return p;
}
//传输集合到前端,json
@RequestMapping("/persons")
public List<Person> showpersons( String name){
System.out.println(name +"社会我赵哥,人怂话还几把多");
Person p1=new Person();
Person p2=new Person();
Person p3=new Person();
p1.setName("fdk");
p1.setHeight(1.75f);
p1.setAge(23);
p2.setName("fdk");
p2.setHeight(1.75f);
p2.setAge(23);
p3.setName("fdk");
p3.setHeight(1.75f);
p3.setAge(23);
List<Person> list=new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
return list;
}
//接收前端传过来的对象数据类型,json
@RequestMapping("/showperson")
public void showperson(@RequestBody Person person){
System.out.println(person);
}
}
注:前端用json传递对象数据,@RequestBody注释必加
前端传的 json格式为
{
"name": "fdk",
"age": 23,
"height": 1.75
}