【问题标题】:Spring rest post service sending null values as variableSpring rest post 服务将空值作为变量发送
【发布时间】: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


【解决方案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;
    }

【讨论】:

  • 试过了,但现在我在邮递员上运行时出现错误“客户端发送的请求在语法上不正确。” ...
  • 您要发送什么请求?我测试了您的代码,根据您上面给出的要求,它对我来说工作正常。我得到的响应是 { "testname": "mynamegotback", "testAge": "myagegotback" }
  • 能否请您将您的整个服务和文件发送给我?如果有的话,你正在使用哪些标题? ...我的要求如下:
  • { "testname": "myname", "testAge": "myage" }
  • 我正在使用与您使用我提供的代码发布的相同的 InputD。标头包含 Content-Type=application/json。你能把你修改后的控制器代码贴出来让我看看吗?
【解决方案2】:

在您的InputDto 类中,您的设置器正确设置了实例变量this.testXxx

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

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

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

【讨论】:

  • @XmlRootElement public class InputDto implements Serializable{ private static final long serialVersionUID = 1L;私有字符串测试名;私人字符串测试年龄; public String getTestname() { return this.testname; } public void setTestname(String testname) { this.testname = testname; } public String getTestAge() { return this.testAge; } public void setTestAge(String testAge) { this.testAge = testAge; } 公共 InputDto() { 超级(); } public InputDto(String testname, String testAge) { this.testname = testname; this.testAge = testAge; }
【解决方案3】:

对不起,伙计们......代码一直在工作,我在邮递员中提交帖子数据时犯了一个非常愚蠢的错误(我选择了错误的选项卡并且没有在“正文”下发送它)......我浪费了 3这几天!!!!..无论如何,再次感谢@abaghel

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2019-12-30
    • 2017-05-04
    • 2011-05-27
    • 2011-08-28
    • 2016-09-24
    相关资源
    最近更新 更多