[英]getting unsupported media type in spring mvc controller
I have written a controller. 我已经写了一个控制器。 I just modified it for accepting a list of object rather than only a object.
我刚刚对其进行了修改,以接受对象列表,而不只是对象。 so I created a wrapper class with list of original objects.
所以我创建了一个包含原始对象列表的包装器类。 But after this i am getting 415 - unsupported media type.
但是之后,我得到415-不支持的媒体类型。 which i am not been able to figure out why its happening.
我无法弄清楚它为什么会发生。 Tried almost all suggestions i could find.
尝试了几乎所有我能找到的建议。
Controller class : 控制器类:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> addCPCodeUser(HttpServletRequest request,
@RequestBody CPCodeUserWrapper users) {
Wrapper class : 包装类:
public class CPCodeUserWrapper {
private List<CPCodeUser> users;
.... setters and getters ...
}
original class : 原始课程:
public class CPCodeUser {
private String userId;
private String name;
List<String> roles;
.... setters and getters....
}
url - v1/users/
JSON :
{
"users" : [{
"userId" : "user1",
"roles" : ["role1", "role2"]
},{
"userId" : "user2",
"roles" : ["role1", "role2"]
}]
}
Do your POJOs have no argument constructors? 您的POJO是否没有参数构造函数? I've had very obscure errors when the JSON library hasn't been able to instantiate the objects and the exceptions get obfuscated by spring.
当JSON库无法实例化对象并且异常在spring之前被混淆时,我遇到了非常模糊的错误。
Are you using the MockMVC framework to test your controllers? 您是否正在使用MockMVC框架来测试您的控制器? I find unit tests often give much clearer exceptions that when the app is deployed
我发现单元测试通常会给出更清晰的例外,即在部署应用程序时
Can you try modifying your CPCodeUser
as follows: 您是否可以尝试按以下方式修改
CPCodeUser
:
public class CPCodeUser {
private String userId;
private List<String> roles;
.... setters and getters....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.