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
Here is the code:
`@RequestMapping("/role/add.action") @ResponseBody public Map