【发布时间】:2017-02-20 13:57:33
【问题描述】:
我是 Spring MVC 的新手,我正在公开 REST 服务。我的 GET 服务运行良好,但来自 Postman 的 POST 调用将空的 null 值作为字段发送。
以下是使用各种形式的POST 注释的示例 REST 服务。在过去的三天里,我一直坚持这一点。请帮忙。
package com.cgi.ehr.rest;
import javax.ejb.EJB;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.cgi.ehr.services.dao.UserDetailsDAO;
import com.cgi.test.dto.InputDto;
@RestController
@RequestMapping("/bookmarks")
public class CheckingService {
@EJB
UserDetailsDAO userDetailsDAO;
@RequestMapping(value = "/bookmark", method = RequestMethod.GET)
public InputDto testM() {
return userDetailsDAO.testMethod();
}
@RequestMapping(value = "/testM2", method = RequestMethod.GET,headers="Accept=*/*", produces="application/json")
//@Produces(MediaType.APPLICATION_JSON)
public InputDto testM2() {
InputDto d = new InputDto("1","2");
return d;
}
@RequestMapping(value = "/bookmarkPost", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
//@ResponseBody
public InputDto testPost(InputDto inputDto) {
String age = inputDto.getTestAge();
String name = inputDto.getTestname();
InputDto dto = new InputDto();
dto.setTestAge(age + "gotback");
dto.setTestname(inputDto.getTestname() + "gotback");
return dto;
}
@RequestMapping(value = "/bookmarkPost1", method = RequestMethod.POST)
public InputDto testPost1(InputDto inputDto) {
String age = inputDto.getTestAge();
String name = inputDto.getTestname();
InputDto dto = new InputDto();
dto.setTestAge(age + "gotback");
dto.setTestname(inputDto.getTestname() + "gotback");
return dto;
}
@RequestMapping(value = "/person", method = RequestMethod.POST, headers = "Accept=application/xml, application/json")
public @ResponseBody InputDto addPerson(@RequestBody InputDto inputDto) {
return inputDto;
}
}
`
package com.cgi.test.dto;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class InputDto implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String testname;
private String testAge;
public String getTestname() {
return testname;
}
public void setTestname(String testname) {
this.testname = testname;
}
public String getTestAge() {
return testAge;
}
public void setTestAge(String testAge) {
this.testAge = testAge;
}
public InputDto() {
super();
}
public InputDto(String testname, String testAge) {
this.testname = testname;
this.testAge = testAge;
}
}
【问题讨论】:
-
输入 json : { "testname": "myname", "testAge": "myage" }
-
输出 json : { "testname": "nullgotback", "testAge": "nullgotback" }
标签: java json spring rest null