未找到 Java 类型的邮件正文编写器 mime 类型 application/x-www-form-urlencoded
A message body writer for Java type was not found mime type application/x-www-form-urlencoded
我知道以前有人问过这个问题,但 none 的答案适合我的情况。所以我不得不寻求帮助。
我是 JERSEY 的新手,我应该制作一个连接到服务并从中获取令牌的休息客户端。
代码如下:
public String getToken() {
String urlToken = "http://example.com";
Client client = Client.create();
Map<String, String> params = new HashMap<String, String>();
params.put("username","username" );
GenericEntity<Map<String, String>> entity = new GenericEntity<Map<String, String>>(params){};
client.addFilter(new LoggingFilter());
WebResource webResource = client.resource(UriBuilder.fromUri(urlToken).build());
String response = webResource
.type("application/x-www-form-urlencoded")
.post(String.class,entity);
return response;
}
错误如下:
HTTP Status 500 - com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, application/x-www-form-urlencoded, was not found
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, application/x-www-form-urlencoded, was not found
com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
com.sun.jersey.api.client.filter.LoggingFilter.handle(LoggingFilter.java:217)
com.sun.jersey.api.client.Client.handle(Client.java:652)
com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
com.sun.jersey.api.client.WebResource.access0(WebResource.java:74)
com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
com.sgss.nove.service.tableau.impl.TableauManager.getToken(TableauManager.java:27)
com.sgss.nove.server.TableauServlet.doGet(TableauServlet.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.sgss.nove.UserSessionControlFilter.doFilter(UserSessionControlFilter.java:130)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
com.sgss.nove.GWTCacheControlFilter.doFilter(GWTCacheControlFilter.java:56)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
您需要使用 MultivaluedMap
,而不是 HashMap
作为参数。他们这样做的原因是因为表单参数键可以用不同的值重复多次。例如 a=1&a=2&a=3
。另外这里不需要GenericEntity
。
MultivaluedMap params = new MultivaluedMapImpl();
params.add("username", "username");
String response = webResource
.type("application/x-www-form-urlencoded")
.post(String.class, params);
我知道以前有人问过这个问题,但 none 的答案适合我的情况。所以我不得不寻求帮助。 我是 JERSEY 的新手,我应该制作一个连接到服务并从中获取令牌的休息客户端。 代码如下:
public String getToken() {
String urlToken = "http://example.com";
Client client = Client.create();
Map<String, String> params = new HashMap<String, String>();
params.put("username","username" );
GenericEntity<Map<String, String>> entity = new GenericEntity<Map<String, String>>(params){};
client.addFilter(new LoggingFilter());
WebResource webResource = client.resource(UriBuilder.fromUri(urlToken).build());
String response = webResource
.type("application/x-www-form-urlencoded")
.post(String.class,entity);
return response;
}
错误如下:
HTTP Status 500 - com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, application/x-www-form-urlencoded, was not found
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.HashMap, and MIME media type, application/x-www-form-urlencoded, was not found
com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
com.sun.jersey.api.client.filter.LoggingFilter.handle(LoggingFilter.java:217)
com.sun.jersey.api.client.Client.handle(Client.java:652)
com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
com.sun.jersey.api.client.WebResource.access0(WebResource.java:74)
com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
com.sgss.nove.service.tableau.impl.TableauManager.getToken(TableauManager.java:27)
com.sgss.nove.server.TableauServlet.doGet(TableauServlet.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.sgss.nove.UserSessionControlFilter.doFilter(UserSessionControlFilter.java:130)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
com.sgss.nove.GWTCacheControlFilter.doFilter(GWTCacheControlFilter.java:56)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
您需要使用 MultivaluedMap
,而不是 HashMap
作为参数。他们这样做的原因是因为表单参数键可以用不同的值重复多次。例如 a=1&a=2&a=3
。另外这里不需要GenericEntity
。
MultivaluedMap params = new MultivaluedMapImpl();
params.add("username", "username");
String response = webResource
.type("application/x-www-form-urlencoded")
.post(String.class, params);