新建maven项目
添加依赖
org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE org.springframework.boot spring-boot-starter-web
一个User类
public class User { private String name; private int age; private String address; 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
一个Controller
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/")public class UserController { @RequestMapping("/testJson") public User testJson() { User user = new User(); user.setName("lijia"); user.setAge(27); user.setAddress("北京"); return user; } }
一个启动类
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ApplicationApp { public static void main(String[] args) { SpringApplication.run(ApplicationApp.class, args); }}
启动,然后输入http://localhost:8080/testJson
是不是很简单
开始我不知道从哪里下手,真好看到了,准备照着da毕竟有大神带路让我少走很多弯路