[英]415 unsupported media type + spring
The problem: 问题:
I'm trying to send a request as a POST
with to parameters: {id: 20, roleName: "ADMIN"}
and getting this error (415 unsupported media type). 我正在尝试将
POST
请求发送到参数为: {id: 20, roleName: "ADMIN"}
并收到此错误(415不支持的媒体类型)。
Framework: 框架:
Spring 4.1.1 春天4.1.1
In my @Controller
in server side, I have the following: 在服务器端的
@Controller
中,我具有以下内容:
@RequestMapping("/role/add.action")
@ResponseBody
public Map<String,Object> addrole(@RequestBody Role role,
HttpServletRequest request,
Authentication authentication,
HttpServletResponse response) {
//Code goes here..
}
This --> @RequestBody Role role
works fine for any other type of object but, for Role
I get this issue. 这个->
@RequestBody Role role
适用于任何其他类型的对象,但是对于Role
我却遇到了这个问题。
My Role
class is: 我的
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...
}
This class has java.util.Set
attributes, and I think that this may causing the problem. 此类具有
java.util.Set
属性,并且我认为这可能会引起问题。
I'm sending just two properties: id and roleName. 我仅发送两个属性:id和roleName。 The cast should work, right?
演员应该工作,对不对?
PS: I've set a Jackson message-converter
bean already, but didn't work. 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>
Someone could help me? 有人可以帮我吗? xD
xD
My problem was basically on @JsonManagedReference
annotation. 我的问题基本上是在
@JsonManagedReference
注释上。
What I did: 我做了什么:
1) I changed my payload to a Map
: 1)我将有效负载更改为
Map
:
public Map<String,Object> addRole(@RequestBody Map payload,
HttpServletRequest request,
Authentication authentication,
HttpServletResponse response) {
...
}
2) Then I tried to cast it with Jackson's ObjectMapper ( com.fasterxml.jackson.databind.ObjectMapper
): 2)然后,我尝试使用Jackson的ObjectMapper(
com.fasterxml.jackson.databind.ObjectMapper
)进行投射:
ObjectMapper mapper = new ObjectMapper();
Role role = mapper.convertValue(payload, Role.class);
Then the debugger threw me an exception: 然后调试器向我抛出了一个异常:
java.lang.IllegalArgumentException: Can not handle managed/back reference 'parent': no back reference property found from type [collection type; class java.util.Set, contains [simple type, class br.com.ttrans.samapp.model.Menu]]
This exception helped find the problem, that (in my case) was the annotation @JsonManagedReference
. 这个异常有助于发现问题,在我的情况下是
@JsonManagedReference
注释。 From docs : 从文档 :
Annotation used to indicate that annotated property is part of two-way linkage between fields;
注释,用于指示带注释的属性是字段之间双向链接的一部分; and that its role is "parent" (or "forward") link.
并且它的角色是“父”(或“前进”)链接。 Value type (class) of property must have a single compatible property annotated with JsonBackReference.
属性的值类型(类)必须具有单个兼容的属性,并带有JsonBackReference注释。 Linkage is handled such that the property annotated with this annotation is handled normally (serialized normally, no special handling for deserialization);
处理链接时,可以正常处理用此注释注释的属性(正常序列化,没有反序列化的特殊处理); it is the matching back reference that requires special handling
匹配后向引用需要特殊处理
This is used for two-way linkage and my payload was coming one-way . 这用于双向链接,而我的有效载荷是单向的 。 So..
所以..
3) ...I removed @JsonManagedReference
's annotations and kept only @JsonBackReference
in all nested classes; 3)...我删除了
@JsonManagedReference
的注释,并在所有嵌套类中仅保留了@JsonBackReference
;
Then the debbuger threw me another exception, but this time was: java.lang.IllegalArgumentException: Unrecognized field ...
so putting @JsonIgnoreProperties(ignoreUnknown = true)
on all classes solved my problem. 然后,调试器向我抛出了另一个异常,但这一次是:
java.lang.IllegalArgumentException: Unrecognized field ...
因此,在所有类上放置@JsonIgnoreProperties(ignoreUnknown = true)
解决我的问题。
So after all that I could receive the payload already parsed as Role
: 所以毕竟我可以收到已经解析为
Role
的有效负载:
@RequestMapping("/role/add.action")
@ResponseBody
public Map<String,Object> addRole(@RequestBody Role role,
HttpServletRequest request,
Authentication authentication,
HttpServletResponse response) {
...
}
Hope it helps! 希望能帮助到你!
This is clearly due to ManyToMany associations (like features,menus etc). 这显然是由于ManyToMany关联(如功能,菜单等)引起的。 Modify such that it breaks the json chain, may be adding @JsonIgnore at suitable places will solve it.
进行修改,使其断开json链,可以在适当的位置添加@JsonIgnore将其解决。
dont use both @JsonBackReference
and @JsonManagedReference
. 不要同时使用
@JsonBackReference
和@JsonManagedReference
。 remove only @JsonManagedReference
annotation. 仅删除
@JsonManagedReference
批注。 it worked for me 它对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.