【问题标题】:I sent correct JSON format body from Postman to my Path but I get 400 Bad Request error我将正确的 JSON 格式正文从 Postman 发送到我的路径,但我收到 400 Bad Request 错误
【发布时间】:2019-06-30 10:46:28
【问题描述】:

我从 Postman 发送了正确的 JSON 格式正文。

{
    "loginId": "xxxxxx",
    "password": "xxxxxx",
    "clientIP": "xxxxx",
    "companyId": "xxxxx"
}

这是我的控制器

@RestController
@RequestMapping(path = "/umm")
   public class LoginServer {
       private transient Logger log = LogManager.getLogger(this.getClass());
       @RequestMapping(value = "/basicLogin",method = RequestMethod.POST)
       public @ResponseBody LoginRequest login(@RequestBody(required = true) LoginRequest loginRequest){
           return null; 
}

}

这是我的域名

public class LoginRequest implements Serializable {

    private static final long serialVersionUID = -884241731093688658L;
    private String loginId;
    private String password;
    private String clientIP;
    private String companyId;

    public LoginRequest(String loginId, String password, String clientIP, String companyId) {
        this.loginId = loginId;
        this.password = password;
        this.clientIP = clientIP;
        this.companyId = companyId;
    }

//getter and setter omitted

}

我不知道为什么它返回 400 错误代码(错误请求),因为我发送了正确的 JSON 正文

这是响应消息。

{
    "timestamp": 1549458416991,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "JSON parse error: Can not construct instance of escf.api.domain.login.LoginRequest: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of escf.api.domain.login.LoginRequest: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@1c360a55; line: 2, column: 2]",
    "path": "/umm/basicLogin"
}

我需要它返回代码 200。

【问题讨论】:

  • 请显示完整的请求和完整的响应,应该有错误信息
  • 好的,现在知道了。
  • 阅读响应中的消息。它会准确地告诉您要解决的问题。
  • 我创建了完全相同的 Controller 和 LoginRequest 类,它对我来说非常好用。
  • 我添加了 super();在域类中它对我有用。

标签: json spring spring-boot postman


【解决方案1】:

鉴于您使用 Jackson 而不是 Gson 进行序列化/反序列化,以下解决方案应该适合您。

public class LoginRequest implements Serializable {

    private static final long serialVersionUID = -884241731093688658L;
    private String loginId;
    private String password;
    private String clientIP;
    private String companyId;

    @JsonCreator
    public LoginRequest(@JsonProperty("loginId") String loginId, @JsonProperty("password") String password, @JsonProperty("clientIP") String clientIP, @JsonProperty("companyId") String companyId) {
        this.setLoginId(loginId);
        this.setPassword(password);
        this.setClientIP(clientIP);
        this.setCompanyId(companyId);
    }

// Define your getters and setters IMPORTANT

}

资源:https://www.baeldung.com/jackson-annotations

【讨论】:

  • 您是否尝试使用我上面的解决方案? ^^^ 谢谢。
【解决方案2】:

添加超级();在域类中它的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 2019-04-20
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多