71.1 编写JSON REST服务

只要添加的有Jackson2依赖,Spring Boot应用中的任何@RestController默认都会渲染为JSON响应,例如:

  1. @RestController
  2. public class MyController {
  3. @RequestMapping("/thing")
  4. public MyThing thing() {
  5. return new MyThing();
  6. }
  7. }

只要MyThing能够通过Jackson2序列化(比如,一个标准的POJO或Groovy对象),默认localhost:8080/thing将响应一个JSON数据。有时在浏览器中你可能看到XML响应,因为浏览器倾向于发送XML accept headers。