Java Spring rest post服务将空值作为变量发送

Java Spring rest post服务将空值作为变量发送,java,json,spring,rest,null,Java,Json,Spring,Rest,Null,我是SpringMVC的新手,我正在公开REST服务。我的GET服务工作正常,但来自邮递员的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.springframewo

我是SpringMVC的新手,我正在公开REST服务。我的
GET
服务工作正常,但来自邮递员的
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;
    }


}

您的testPost方法应该如下所示。现在,您可以使用相同的请求使用邮递员

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

InputDto
类中,设置程序正确地设置了实例变量
this.testXxx

public void setTestname(String testname) {
    this.testname = testname;
}
但是,getter不会返回此实例变量。请注意您缺少的
此项。
以下内容应该可以修复您当前的问题,然后应用于两个getter

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

抱歉,伙计们…代码一直在工作,我在postman中提交帖子数据时犯了一个非常愚蠢的错误(我选择了错误的标签,没有在“正文”下发送它)。。。我在这上面浪费了3天!!!!。。无论如何,再次感谢@abaghel

input json:{“testname”:“myname”、“testAge”:“myage”}output json:{“testname”:“nullgotback”、“testAge”:“nullgotback”}@XmlRootElement公共类inputdt实现可序列化{private static final long serialVersionUID=1L;private String testname;private String testAge;public String getTestname(){返回this.testname;}public void setTestname(String testname){this.testname=testname;}public String getestage(){返回this.testAge;}public void setteststage(String testAge){this.testAge=testAge;}public InputDto(){super();}public InputDto(String testname,String testAge){this.testname=testname;this.testAge=testAge;}尝试过这个方法,但现在在postman上运行时出现错误“客户端发送的请求在语法上不正确。”…您发送的请求是什么?我测试了您的代码,它与您上面给出的请求配合良好。我得到的响应是{“testname”:“mynamegotback”,“testAge”:“myagegotback”}您可以将您的整个服务和文件发送给我吗?如果有,您使用的是哪些头?…我的请求如下:{“testname”:“myname”,“testAge”:“myage”}我正在使用与我提供的代码相同的输入。标题包含Content Type=application/json。请发布修改后的控制器代码,以便我查看一下?