Spring rest post service将null值作为变量发送

时间:2016-09-04 08:54:41

标签: java json spring rest null

我是Spring MVC的新手,我正在公开REST服务。我的GET服务工作正常,但Postman的POST调用将空null个值作为字段发送。

以下是使用各种形式的POST注释的示例REST服务。在过去的3天里,我一直坚持这一点。请帮忙。

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;
    }


}

enter image description here

3 个答案:

答案 0 :(得分:1)

您的testPost方法应如下所示。现在您可以使用Postman使用相同的请求。

          @RequestMapping(value = "/bookmarkPost", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
          public @ResponseBody InputDto testPost(@RequestBody InputDto inputDto) {
               String age = inputDto.getTestAge();
               String name = inputDto.getTestname();
               InputDto dto = new InputDto();
               dto.setTestAge(age + "gotback");
               dto.setTestname(name + "gotback");
               return dto;
    }

答案 1 :(得分:0)

InputDto班级中,您的二传手正确设置了实例变量this.testXxx

public void setTestname(String testname) {
    this.testname = testname;
}

但是,您的getter不会返回此实例变量。请注意您遗失的this.以下内容应解决您当前的问题,然后应用于两个getter。

public String getTestAge() {
    return this.testAge;
}

答案 2 :(得分:0)

抱歉,伙计......代码一直在运作,我在邮递员提交帖子数据时犯了一个非常愚蠢的错误(我选择了错误的标签而没有在" body"下发送)。 ..我浪费了3天!!!! ..反正再次感谢@abaghel