尝试使用 POST 方法执行宁静的 Web 服务。这是我界面的相关部分:
@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 - 未找到)在 org.apache.wink.server.internal 的处理程序链调用 javax.ws.rs.WebApplicationException 期间发生。 handlers.FindRootResourceHandler.handleRequest(FindRootResourceHandler.java:60) 在 org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) 在 org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain. java:22) 在 org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) 在 org.apache.wink.server.internal.handlers.HeadMethodHandler.handleRequest(HeadMethodHandler.java:47) 在 org .apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) 在 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) 在 org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) 在 org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22) 在 org .apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) 在 org.apache.wink.server.internal.handlers.SearchResultHandler.handleRequest(SearchResultHandler.java:33) 在 org.apache.wink.server .handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26) 在 org.apache.wink.server.handlers.RequestHandlersChain。org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:55) 的句柄(RequestHandlersChain.java:22) org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain.java:43)在 org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:136) 在 org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:117) 在 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 ) 在 org.apache.catalina.core.StandardWrapperValve 的 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)。在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:203) 调用(StandardWrapperValve.java:240) .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) 在 org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:281) 在 org.apache.coyote.http11 的 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)。 Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357) 在 org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1671) 在 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 严重:MediaType 的 MediaType 格式错误:"; q=.2" 2011 年 1 月 12 日上午 9:47:35 org.apache.wink.server.internal.RequestProcessor handleRequest 严重:未处理的异常 java.lang.IllegalArgumentException:MediaType 的 MediaType 格式错误:"; q=.2" 在 org.apache.wink.common.internal.providers.header.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:67) 在 org.apache.wink.common.internal.providers.header.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate .java:32) 在 javax.ws.rs.core.MediaType.valueOf(MediaType.java:119) 在 org.apache.wink.common.internal.providers.header.AcceptHeaderDelegate.fromString(AcceptHeaderDelegate.java:47) 在org.apache.wink.common.internal.providers.header.AcceptHeaderDelegate.fromString(AcceptHeaderDelegate.java:32) 在 org.apache.wink.common.internal.http.Accept.valueOf(Accept.java:139) 在 org. apache.wink.server.internal.contexts.HttpHeadersImpl.getAcceptHeader(HttpHeadersImpl.java:115) 在 org.apache.wink.server.internal.contexts.HttpHeadersImpl.getAcceptableMediaTypes(HttpHeadersImpl.java: