RestController有时返回xml有时是json

时间:2017-12-12 12:29:29

标签: java json xml spring jackson

我在课程中听说如果你用 @XmlRootElement 注释你的POJO,当你从 Restcontroller 返回POJO或pojos列表时,您可以选择以JSON格式(通过杰克逊库)或XML格式(通过JAXB)获取它。 获得两种不同反应的方法就是:

http://localhost:8080/tasks       //default method
http://localhost:8080/tasks.json  //JSON response
http://localhost:8080/tasks.xml   //XML response

我的Spring Boot项目的奇怪之处在于我用@XmlRootElement注释了我的任务POJO,但这些是我的HTTP请求的结果:

http://localhost:8080/tasks      -> I get JSON list of Tasks
http://localhost:8080/tasks.json -> I get JSON list of Tasks
http://localhost:8080/tasks.xml  -> I get Whitelabel error page

http://localhost:8080/tasks/3      -> I get XML list of Tasks
http://localhost:8080/tasks/3.json -> I get Whitelabel error page
http://localhost:8080/tasks/3.xml  -> I get XML list of Tasks

事情很奇怪,因为.xml和.json交替工作。 我试图用@XmlElement注释每个Task.java属性,但没有任何改变。我试图在我的pom.xml中明确添加JAXB依赖项(即使在Spring Boot Starter Web中已经存在),但没有任何变化。我已尝试用@Controller + @ResponseBody替换我的@Restcontroller请求映射,但没有任何变化。

这是我的POJO Task.java

package com.quicktutorialz.learnmicroservices.XmlVsJsonData.entities;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

@Entity @Table(name="tasks")
@AllArgsConstructor @NoArgsConstructor
@XmlRootElement
public class Task {

  @Id @GeneratedValue @Column(name="ID")
  @Getter @Setter
  private Integer id;                         //it can be null because it's autogenerated and I don't insert it for databinding

  @Column(name="TITLE")
  @Getter @Setter
  @NotBlank
  private String title;

  @Column(name="DATE")
  @Getter @Setter
  private Date date;                          //it can be null because I use pre-persist and I don't insert it for data binding

  @Column(name="USER_ID")
  @Getter @Setter
  @NotNull
  private Integer fkUser;

  @PrePersist
  private void setDate(){
     this.date = new Date();
  }
}

这是我的Spring RestController:

package com.quicktutorialz.learnmicroservices.XmlVsJsonData.controllers;

import 
com.quicktutorialz.learnmicroservices.XmlVsJsonData.daos.TaskDao;
import com.quicktutorialz.learnmicroservices.XmlVsJsonData.daos.UserDao;
import com.quicktutorialz.learnmicroservices.XmlVsJsonData.entities.Task;
import com.quicktutorialz.learnmicroservices.XmlVsJsonData.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@org.springframework.web.bind.annotation.RestController
public class RestController {

  @Autowired TaskDao taskDao;
  @Autowired UserDao userDao;

  @RequestMapping("/tasks")
  public List<Task> getAllTasks(){
     return taskDao.findAll();
  }

  @RequestMapping("/tasks/{id}")
  public Task getATask(@PathVariable(value = "id") Integer taskId){
     return taskDao.getOne(taskId);
  }

  @RequestMapping("/users")
  public List<User> getAllUsers(){
     return userDao.findAll();
  }

  @RequestMapping("/users/{id}")
  public User getAUser(@PathVariable(value = "id") Integer userId){
     return userDao.getOne(userId);
  }
}

有什么不对?

0 个答案:

没有答案