支持 "PATCH" 动词 - WSHttpHandler handleExchange 给出警告:无法处理 HTTP 方法:PATCH
Supporting the "PATCH" verb - WSHttpHandler handleExchange gives WARNING: Cannot handle HTTP method: PATCH
我创建了一个 class,它使用 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 控制台中收到以下警告:
Jul 30, 2019 3:39:15 PM
com.sun.xml.internal.ws.transport.http.server.WSHttpHandler
handleExchange WARNING: Cannot handle HTTP method: PATCH
以及对我的 curl
请求的以下响应:
curl: (52) Empty reply from server
查看 here,在 WSHTTPHandler
class 中,我可以看出问题出在哪里:
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) 我可以用我自己的自定义 class 替换 WSHTTPHandler
吗?如果是这样,我如何告诉 Endpoint
我想使用它?
或 b) 是否有 WSHttpHandler
的更新版本、更现代的替代方法或创建我可以使用的 web 服务的不同方法?
此处不支持 PATCH
- 应改用其他处理程序。
我创建了一个 class,它使用 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 控制台中收到以下警告:
Jul 30, 2019 3:39:15 PM com.sun.xml.internal.ws.transport.http.server.WSHttpHandler handleExchange WARNING: Cannot handle HTTP method: PATCH
以及对我的 curl
请求的以下响应:
curl: (52) Empty reply from server
查看 here,在 WSHTTPHandler
class 中,我可以看出问题出在哪里:
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) 我可以用我自己的自定义 class 替换 WSHTTPHandler
吗?如果是这样,我如何告诉 Endpoint
我想使用它?
或 b) 是否有 WSHttpHandler
的更新版本、更现代的替代方法或创建我可以使用的 web 服务的不同方法?
PATCH
- 应改用其他处理程序。