【发布时间】:2015-09-17 10:20:57
【问题描述】:
问题:
我正在尝试以 POST 的形式发送请求,参数为:{id: 20, roleName: "ADMIN"} 并收到此错误(415 不受支持的媒体类型)。
框架:
春季4.1.1
在我的服务器端@Controller 中,我有以下内容:
@RequestMapping("/role/add.action")
@ResponseBody
public Map<String,Object> addrole(@RequestBody Role role,
HttpServletRequest request,
Authentication authentication,
HttpServletResponse response) {
//Code goes here..
}
这 --> @RequestBody Role role 适用于任何其他类型的对象,但对于 Role 我遇到了这个问题。
我的Role 班级是:
@Entity
public class Role implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private int id;
@Column
private String roleName;
@Fetch(FetchMode.SELECT)
@OneToMany(mappedBy = "role", targetEntity = Users.class, fetch = FetchType.EAGER)
@JsonManagedReference(value="role")
private List<Users> users = new LinkedList<Users>();
@Fetch(FetchMode.SELECT)
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="role_features",
joinColumns=@JoinColumn(name="roleId"),
inverseJoinColumns=@JoinColumn(name="featureId"))
@OrderBy(clause="featureId")
private Set<SystemFeature> features = new HashSet<SystemFeature>();
@Fetch(FetchMode.SELECT)
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="role_menu",
joinColumns=@JoinColumn(name="roleId"),
inverseJoinColumns=@JoinColumn(name="menuId"))
@OrderBy(clause="menuId")
private Set<Menu> menus = new HashSet<Menu>();
@Fetch(FetchMode.SELECT)
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="role_services_stations",
joinColumns=@JoinColumn(name="roleId"),
inverseJoinColumns=@JoinColumn(name="stationId"))
@OrderBy(clause="stationId")
private Set<ServiceStation> stations = new HashSet<ServiceStation>();
//Constructors, getters and setters...
}
这个类有java.util.Set属性,我认为这可能会导致问题。
我只发送两个属性:id 和 roleName。演员应该可以工作,对吧?
PS:我已经设置了一个 Jackson message-converter bean,但是没有用。
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
有人可以帮助我吗? xD
【问题讨论】:
-
你试过
{"id": 20, "roleName": "ADMIN"}吗? (属性名称中的双引号)。还可以尝试将Content-Type标头设置为application/json。 -
换成
@RequestMapping(value="/role/add.action", consumes = "application/json")怎么办 -
@orid 有效负载已正确发送,并且在开发者工具上的 Content-Type 是 'Content-Type:application/json'。 =/
-
@Magnamag 我做了你说的改变,但也没有用..
-
好的,你的控制器是返回
null还是一个空值?