【发布时间】:2015-03-08 06:51:06
【问题描述】:
当我在@RequestBody 中使用具有继承的类时,我收到“400 Bad Request”。我有另一个控制器,我在其中使用没有继承的类并且它可以工作。
我想知道我是否可以在@RequestBody 中使用常规EJB,或者对可以使用的类有什么限制?
下面是控制器:
@RequestMapping(value = "auth/ping", method = RequestMethod.POST, consumes="application/json")
@ResponseStatus(HttpStatus.OK)
public @ResponseBody String pingPost(@RequestBody User model) {
final String signature = CLASSNAME + "#pingPost(User model)";
Helper.logEntrance(getLogger(), signature, new String[]{"model"}, new Object[]{model});
return "unprotected ping post with data";
}
以下是类
public abstract class User extends AuditableEntity {
@Indexed
private String name;
private String password;
public User() {
super();
}
public String getName() {
return this.name;
}
public void setName(String value) {
this.name = value;
}
public String getPassword() {
return this.password;
}
public void setPassword(String value) {
this.password = value;
}
}
public abstract class AuditableEntity {
private User createdBy;
private Date createdDate;
public AuditableEntity() {
super();
}
public User getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(User value) {
this.createdBy = value;
}
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date value) {
this.createdDate = value;
}
}
我在 maven 中包含以下库
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
【问题讨论】:
-
由于您提到 @RequestBody 适用于其他场景,我假设 Spring 配置是正确的。所以我会检查Json对象的格式是否正确。
-
另外,打开 MVC 日志以进行调试,并找出错误的确切内容和位置。
标签: json spring spring-mvc model-view-controller