2011-01-12 24 views
1

嘗試使用POST方法執行寧靜的Web服務。這裏是我的界面的相關部分:restful調用MediaType的錯誤MediaType格式:「*; q = .2」

@Path("/customers") 
public interface CustomerResource { 
@POST 
@Consumes(MediaType.APPLICATION_XML) 
public Response createCustomer(InputStream is); 
     ..... 
} 

實施:

// 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(); 
} 

客戶端發出的呼叫(相關部分)

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(); 

這裏是我的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> 

當我做交流所有我回來的是巨大的異常女巫實際上是說: INFO:處理程序鏈調用期間發生WebApplicationException(404 - 未找到) 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.RequestHandl ersChain.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.apac he.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) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) 在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(未知源) 2011年1月12日上午9:47:35 org.apache.wink.common.internal.providers.heade r.MediaTypeHeaderDelegate fromString SEVERE:媒體類型錯誤的MediaType格式:「; q = .2「 2011年1月12日上午9:47:35 org.apache.wink.server.internal.RequestProcessor handleRequest SEVERE:未處理的異常 java.lang.IllegalArgumentException:媒體類型的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.inte rnal.contexts.HttpHeadersImpl.getAcceptableMediaTypes(HttpHeadersImpl.java:84) 在org.apache.wink.server.internal.handlers.PopulateResponseMediaTypeHandler.handleResponse(PopulateResponseMediaTypeHandler.java:82) 在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) 在org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) 在org.apache.wink.server.handlers.AbstractHandler.handleResponse(AbstractHandler.java:39) 在org.apache.wink.server.handlers.ResponseHandlersChain.handle(ResponseHandlersChain.ja va: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。運行(AbstractHandlersChain.java:43) at org.apache.wink.server.internal.RequestProcessor.handleRequestWith outFaultBarrier(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 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain。的java:304) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 在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) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在org.apache。 catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) at org.apache.coyote.ht tp11.Http11AprProcessor.process(Http11AprProcessor.java:281) at org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process(Http11AprProtocol.java:357) at org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor。運行(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。運行(來源不明) 產生的原因:java.lang.ArrayIndexOutOfBoundsException:1 在org.apache.wink.common.internal.providers.header.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:54) ...... 40多個

+0

異常在哪裏? – Anders 2011-01-12 15:31:12

+0

對不起,我不知道爲什麼沒有發佈。我再次添加它。 – BigWonder 2011-01-12 17:04:04

回答

1

正如我在客戶端代碼中看到的那樣,您註釋了內容類型。
爲什麼?你發送什麼類型的內容?
無論如何正確的媒體類型應該是*/*;q=0.2

順便說一句,你使用什麼版本的Wink?我認爲最新版本對於媒體類型錯誤更容易原諒。

0

此錯誤是由jar文件衝突引起的。我刪除了jetty.jar,jetty-util.jar並刪除了jsr305.jar,REST API也正常工作。 希望這可以幫助你。

相關問題