以下是涉及的课程。
public class UserAuthDto {
private String username;
private String password;
private List<String> roles;
private String tenantCode;
public UserAuthDto(String username,
String password,
List<String> roles,
String tenantCode){
this.username=username;
this.password=password;
this.roles=roles;
this.tenantCode=tenantCode;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
public void setTenantCode(String tenantCode) {
this.tenantCode = tenantCode;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public List<String> getRoles() {
return roles;
}
public String getTenantCode() {
return tenantCode;
}
}
另一个DTO
public class UserAuthRequest {
private UserAuthDto userAuthDto;
public UserAuthRequest(UserAuthDto authDto){
if(authDto == null){
throw new IllegalArgumentException("User Authentication DTO Should Not be null");
}
this.userAuthDto = authDto;
}
public String getUsername(){
return userAuthDto.getUsername();
}
public String getPassword(){
return userAuthDto.getPassword();
}
public List<String> getRoles(){
return userAuthDto.getRoles();
}
public String getTenantCode(){
return userAuthDto.getTenantCode();
}
public void setUsername(String username) {
this.userAuthDto.setUsername(username);
}
public void setPassword(String password) {
this.userAuthDto.setPassword(password);
}
public void setRoles(List<String> roles) {
this.userAuthDto.setRoles(roles);
}
public void setTenantCode(String tenantCode) {
this.userAuthDto.setTenantCode(tenantCode);
}
}
这是处理请求的Controller。
@RequestMapping(value = "authenticate", method = RequestMethod.POST,consumes = "application/json",produces = "application/json")
public UserAuthResponse authenticate(@RequestBody UserAuthRequest authRequest){
//.
}
但是当我发送此数据时
{"username": "test","password":"test","roles":null,"tenantCode":"test"}
我收到400个错误请求。知道为什么吗?
答案 0 :(得分:5)
Spring使用Jackson将请求中的JSON映射到Java对象,以作为参数传递给处理程序方法。
以下是the JSON types。以下是Jackson如何进行JSON映射:
Map
或自定义类对象
Collection
类型(List
,Set
等。)int
,double
,Long
等。原始类型和引用类型)String
对象(如果JSON字符串是键,则为字段)null
false
和true
。您提供了以下JSON
{"username": "test","password":"test","roles":null,"tenantCode":"test"}
这是一个JSON对象,具有4个键值对,其中键为username
,password
,roles
和tenantCode
。因此,您需要一个带有这些名称的字段(或属性)的Java类。你有这样一个班级UserAuthDto
。这就是你应该用作参数类型的东西。
您可以通过在类路径上提供适当的slf4j和logback或log4j库以及Spring的记录器的适当配置来启用日志记录。
答案 1 :(得分:0)
今天早上我遇到了类似的问题。即使我发送的对象是正确的,我也得到了400。我的班级是这样的:
class myClass {
AnalysisResult average; //AnalysisResult is a List<double>
AnalysisResult worstCase;
public MyClass(AnalysisResult average, AnalysisResult worstCase) {
//initializations
}
}
通过添加默认构造函数解决了这个问题:
class myClass {
AnalysisResult average; //AnotherClass is a List<double>
AnalysisResult worstCase;
public MyClass(AnalysisResult average, AnalysisResult worstCase) {
//initializations
}
public MyClass() {}
}
似乎对于某些类,jackson需要一个默认构造函数