找不到类com.sun.jersey.core.util.MultivaluedMapImpl和MIME媒体类型application / json

时间:2014-09-11 23:04:47

标签: java json maven jersey

这是我的测试

final Client client = Client.create();
final WebResource webResource = client.resource(ORDER_API);
final MultivaluedMap<String, String> formData = new MultivaluedMapImpl();
formData.add("name", "Autobot-00");
formData.add("resExpiryKey", "19000");

final ClientResponse response = webResource.type(MediaType.APPLICATION_JSON)
        .header("Content-Type", MediaType.APPLICATION_JSON).post(ClientResponse.class, formData);
assertEquals(response.getStatus(), 200);

我的pom.xml看起来像

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.18.1</version>
    <scope>test</scope>
</dependency>  

当我运行测试时,我看到以下

com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.core.util.MultivaluedMapImpl, and MIME media type, application/json, was not found
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
    at com.sun.jersey.api.client.Client.handle(Client.java:652)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)

缺少什么?

1 个答案:

答案 0 :(得分:1)

使用多值地图时,您应该使用

.type(MediaType.APPLICATION_FORM_URLENCODED)
而不是 .type(MediaType.APPLICATION_JSON)