问题
Trying to execute restful web service using POST method. Here is the relevant part of my interface:
@Path("/customers")
public interface CustomerResource {
@POST
@Consumes(MediaType.APPLICATION_XML)
public Response createCustomer(InputStream is);
.....
}
The implementation:
// Create customer
public Response createCustomer(InputStream is) {
logger.debug("In createCustomer");
Customer customer = readCustomer(is);
customer.setId(idCounter.incrementAndGet());
customerDB.put(customer.getId(), customer);
logger.debug("Created customer " + customer.getId());
logger.debug("Out createCustomer");
return Response.created(URI.create("/customers/"
+ customer.getId())).build();
}
The client makes a call (relevant part)
URL postUrl = new URL("http://localhost:8080/ShoppingApplication/rest/customers");
HttpURLConnection connection =
(HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");
OutputStream os = connection.getOutputStream();
os.write(newCustomer.getBytes());
os.flush();
Here is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>My Shop</display-name>
<servlet>
<servlet-name>MyShopping</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.restfully.shop.services.ShoppingApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyShopping</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
When I make a call all I have back is huge exception witch is actually says: INFO: WebApplicationException (404 - Not Found) occured during the handlers chain invocation javax.ws.rs.WebApplicationException at org.apache.wink.server.internal.handlers.FindRootResourceHandler.handleRequest(FindRootResourceHandler.java:60) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) at org.apache.wink.server.internal.handlers.HeadMethodHandler.handleRequest(HeadMethodHandler.java:47) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) at org.apache.wink.server.internal.handlers.OptionsMethodHandler.handleRequest(OptionsMethodHandler.java:42) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) at org.apache.wink.server.internal.handlers.SearchResultHandler.handleRequest(SearchResultHandler.java:33) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) at org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain.java:43) at org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:136) at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:117) at org.apache.wink.server.internal.servlet.RestServlet.service(RestServlet.java:102) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:203) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:281) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1671) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Jan 12, 2011 9:47:35 AM org.apache.wink.common.internal.providers.header.MediaTypeHeaderDelegate fromString SEVERE: Wrong MediaType format for MediaType:"; q=.2" Jan 12, 2011 9:47:35 AM org.apache.wink.server.internal.RequestProcessor handleRequest SEVERE: Unhandled exception java.lang.IllegalArgumentException: Wrong MediaType format for MediaType:"; q=.2" at org.apache.wink.common.internal.providers.header.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:67) at org.apache.wink.common.internal.providers.header.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:32) at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119) at org.apache.wink.common.internal.providers.header.AcceptHeaderDelegate.fromString(AcceptHeaderDelegate.java:47) at org.apache.wink.common.internal.providers.header.AcceptHeaderDelegate.fromString(AcceptHeaderDelegate.java:32) at org.apache.wink.common.internal.http.Accept.valueOf(Accept.java:139) at org.apache.wink.server.internal.contexts.HttpHeadersImpl.getAcceptHeader(HttpHeadersImpl.java:115) at org.apache.wink.server.internal.contexts.HttpHeadersImpl.getAcceptableMediaTypes(HttpHeadersImpl.java:84) at org.apache.wink.server.internal.handlers.PopulateResponseMediaTypeHandler.handleResponse(PopulateResponseMediaTypeHandler.java:82) at org.apache.wink.server.handlers.AbstractHandler.handleResponse(AbstractHandler.java:38) at org.apache.wink.server.handlers.ResponseHandlersChain.handle(ResponseHandlersChain.java:26) at org.apache.wink.server.handlers.ResponseHandlersChain.handle(ResponseHandlersChain.java:22) at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) at org.apache.wink.server.handlers.AbstractHandler.handleResponse(AbstractHandler.java:39) at org.apache.wink.server.handlers.ResponseHandlersChain.handle(ResponseHandlersChain.java:26) at org.apache.wink.server.handlers.ResponseHandlersChain.handle(ResponseHandlersChain.java:22) at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) at org.apache.wink.server.handlers.AbstractHandler.handleResponse(AbstractHandler.java:39) at org.apache.wink.server.handlers.ResponseHandlersChain.handle(ResponseHandlersChain.java:26) at org.apache.wink.server.handlers.ResponseHandlersChain.handle(ResponseHandlersChain.java:22) at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) at org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain.java:43) at org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:145) at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:117) at org.apache.wink.server.internal.servlet.RestServlet.service(RestServlet.java:102) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:203) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:281) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1671) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 at org.apache.wink.common.internal.providers.header.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:54) ... 40 more
回答1:
As I see in your client code, you commented out the content type.
Why? What content type do you send?
Anyway correct media type should be */*;q=0.2
Btw, what version of Wink do you use? I think the latest version is more forgivable about media types mistakes.
回答2:
This error is caused by the jar file conflict.I deleted jetty.jar,jetty-util.jar and also deleted jsr305.jar,the REST API just work fine. Hope this can help you.
来源:https://stackoverflow.com/questions/4670491/restful-call-wrong-mediatype-format-for-mediatype-q-2