Reputation: 812
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).
Framework:
Spring 4.1.1
In my @Controller
in server side, I have the following:
@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.
My Role
class is:
@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.
I'm sending just two properties: id and roleName. The cast should work, right?
PS: I've set a Jackson message-converter
bean already, but didn't work.
<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
Upvotes: 1
Views: 4507
Reputation: 656
dont use both @JsonBackReference
and @JsonManagedReference
. remove only@JsonManagedReference
annotation. it worked for me
Upvotes: 1
Reputation: 812
My problem was basically on @JsonManagedReference
annotation.
What I did:
1) I changed my payload to a 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
):
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
. 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. 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;
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.
So after all that I could receive the payload already parsed as Role
:
@RequestMapping("/role/add.action")
@ResponseBody
public Map<String,Object> addRole(@RequestBody Role role,
HttpServletRequest request,
Authentication authentication,
HttpServletResponse response) {
...
}
Hope it helps!
Upvotes: 8
Reputation: 1948
This is clearly due to ManyToMany associations (like features,menus etc). Modify such that it breaks the json chain, may be adding @JsonIgnore at suitable places will solve it.
Upvotes: 0