gpt4 book ai didi

java - 支持 "PATCH"动词 - WSHttpHandler handleExchange 给出警告 : Cannot handle HTTP method: PATCH

转载 作者:行者123 更新时间:2023-12-02 02:21:07 30 4
gpt4 key购买 nike

我创建了一个使用 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 类中,我可以看到问题出在哪里:

    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 - 应使用其他处理程序。

关于java - 支持 "PATCH"动词 - WSHttpHandler handleExchange 给出警告 : Cannot handle HTTP method: PATCH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57274994/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com