访问后RESTful请求的正文

时间:2011-03-05 11:44:07

标签: java rest http-post

我可以使用完全阅读帖子请求(标题+原始正文)  com.sun.net.httpserver.HttpServer为其设置了HttpHandler。

但我不能使用javax.xml.ws.WebServiceProvider做同样的事情 我可以读取标题,但我需要请求中的原始主体数据是纯文本,xml还是数据字节。一个InputStreamReader就可以了。

   @ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE) 
    @WebServiceProvider
    @BindingType(value = HTTPBinding.HTTP_BINDING) 
    public class MyRestHandler 
    implements Provider<Source> {

       @Resource
       protected WebServiceContext wsContext; // injected


       public Source invoke(Source source) {

          /* source happens to be null except for text/xml BODY type
           * in the incoming request.
           * In this case, source is a SAXSource*/

       }

    }

附录(在Peter Knego观察之后)

Java Web Services第126-138页中,作者提供了一个RESTful Web服务 使用JAX-WS的示例:

@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE) 
@WebServiceProvider
@BindingType(value = HTTPBinding.HTTP_BINDING) 
public class RestfulTeams 
        implements Provider<Source> {  ... }

甚至,在页面136-137中,它扩展它以支持POST方法,访问 请求的正文。显然,它适用于JAX-WS库类,因为它在body请求中使用xml。现在我理解你的惊讶,以及为什么source总是为null但对于text / xml体。真的,最简单的方法是使用JAX-RS库。

1 个答案:

答案 0 :(得分:2)

使用JAX-RS实施,例如Jersey。书: RESTful Java