我创建了一个使用javax.xml.ws.Endpoint创建REST端点的类:

@WebServiceProvider
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)
@BindingType(value = HTTPBinding.HTTP_BINDING)
public class SpecificRestAPI implements Provider<Source>
{
    // arg 0: url including port, e.g. "http://localhost:9902/specificrestapi"
    public static void main(String[] args)
    {
        String url = args[0];
        // Start
        Endpoint.publish(url, new SpecificRestAPI());
    }

    @Resource
    private WebServiceContext wsContext;


    @Override
       public Source invoke(Source request)
       {
          if (wsContext == null)
             throw new RuntimeException("dependency injection failed on wsContext");
          MessageContext msgContext = wsContext.getMessageContext();
          switch (((String) msgContext.get(MessageContext.HTTP_REQUEST_METHOD)).toUpperCase().trim())
          {
             case "DELETE":
                 return processDelete(msgContext);
             case "PATCH" :
                 return processPatch(msgContext);
'etc...


问题是,当我在Eclipse中运行此应用程序并使用curl通过以下命令使用PATCH对其进行请求时:

curl -i -X PATCH http://localhost:9902/specificrestapi?do=action


我在Eclipse控制台中收到以下警告:


2019年7月30日3:39:15 PM
com.sun.xml.internal.ws.transport.http.server.WSHttpHandler
handleExchange警告:无法处理HTTP方法:PATCH


以下是对我的curl请求的响应:


curl:(52)来自服务器的空回复


here,在WSHTTPHandler类中,我可以看到问题所在:

    private void handleExchange(HttpExchange msg) throws IOException {
    WSHTTPConnection con = new ServerConnectionImpl(adapter,msg);
    try {
        if (fineTraceEnabled) {
            LOGGER.log(Level.FINE, "Received HTTP request:{0}", msg.getRequestURI());
        }
        String method = msg.getRequestMethod();
        ' THIS IS THE PROBLEM - IT DOESN'T KNOW ABOUT PATCH!
        if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD)
        || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) {
            adapter.handle(con);
        } else {
            if (LOGGER.isLoggable(Level.WARNING)) {
                LOGGER.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method));
            }
        }
    } finally {
        msg.close();
    }
}


那么,我有什么选择?
a)我可以用自己的自定义类替换WSHTTPHandler吗?如果是的话,如何告诉我要使用它的Endpoint
或b)是否有更新版本的WSHttpHandler,更现代的替代方法,或者使用其他可用来创建Web服务的方法来实现此目的?

最佳答案

这里不支持PATCH-应该使用其他处理程序。

09-04 13:45