Jersey例外:未找到Java类和MIME媒体类型application / x-www-form-urlencoded的邮件正文阅读器

时间:2015-12-29 13:38:06

标签: java json rest jersey apache-httpclient-4.x

我有一个基于Jersey的Rest WS输出JSON。我正在实现一个HttpClient来调用WS并使用JSON响应。我的客户代码在

之下
public static String sendPostRequest(String url, String accept,
        String authorization, Map<String, String> body)
        throws UnsupportedOperationException, IOException {

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);
    if (authorization != null && !authorization.equals("")) {
        post.setHeader("Authorization", "Bearer " + authorization);
        // post.setHeader("access_token", authorization);
    }

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> map : body.entrySet()) {
        String key = map.getKey();
        String value = map.getValue();
        urlParameters.add(new BasicNameValuePair(key, value));
    }

    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
            urlParameters);
    post.setEntity(urlEncodedFormEntity);

    HttpResponse response = client.execute(post);
    System.out.println("Response Code : "
            + response.getStatusLine().getStatusCode());
    String json = EntityUtils.toString(response.getEntity());
    System.out.println("JSON : " + json);
    return json;
}

我将访问令牌作为标头发送。如果我对此curl -X POST http://192.168.33.70:8080/management/orgs/org1/apps?access_token=YWMtXh3LwK4XEeWaSPc_E_-PwQAAAVIRV758KgNhJ-y3wNNAx5MLSmwTQvi1OPg -d '{"name":"app1"}'使用curl命令,我会得到正确的响应,但是通过HttpClient我得到以下异常堆栈跟踪:

2015-12-29 12:49:18 ERROR          rest.exceptions.AbstractExceptionMapper.toResponse(72)<http-bio-8080-exec-5>- javax.ws.rs.WebApplicationException Server Error (500)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body reader for Java class java.util.Map, and Java type java.util.Map<java.lang.String, java.lang.Object>, and MIME media type application/x-www-form-urlencoded was not found.

    at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:477)
    at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
    at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:86)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:137)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:137)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:137)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:927)
    at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:875)
    at com.sun.jersey.spi.container.servlet.ServletContainer.doFilter(ServletContainer.java:829)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
    at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
    at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
    at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
    at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.usergrid.rest.filters.ContentTypeFilter.doFilter(ContentTypeFilter.java:92)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.jersey.api.MessageException: A message body reader for Java class java.util.Map, and Java type java.util.Map<java.lang.String, java.lang.Object>, and MIME media type application/x-www-form-urlencoded was not found.

我的请求中有正确​​的MIME类型。我的POJO已使用XMLRootElement注释。任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

    Caused by: com.sun.jersey.api.MessageException: A message body reader for Java class java.util.Map, and Java type java.util.Map<java.lang.String, java.lang.Object>, and MIME media type application/x-www-form-urlencoded was not found.

正如异常消息所示,您正在尝试将Marshall / Unmarshall Map数据类型映射到JSON,而Jersey中默认不支持该对象,因为对象的编组/解组由不支持映射的JAXB处理。但是,谷歌可以通过GSON覆盖实施,以注册MessageBodyReader / Writer。可以找到使用带有Jersey的GSON的简单实现here。除此之外,还有许多可在互联网上使用Jersey的GSON实现。

我更感兴趣的是 UrlEncodedFormEntity 类的代码。请发布以获得更多见解。