我从邮递员向我的路径发送了正确的JSON格式的正文,但出现400错误的请求错误

时间:2019-02-06 13:05:23

标签: json spring spring-boot 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。

2 个答案:

答案 0 :(得分:0)

添加super();在域类中有效。

答案 1 :(得分:0)

鉴于您使用的是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