gpt4 book ai didi

java - Apache CXF - 以 http 为中心的方法和从客户端到服务器的 PUT

转载 作者:可可西里 更新时间:2023-11-01 16:34:42 25 4
gpt4 key购买 nike

我使用 Apache CXF 提供与 RESTful API 的通信。

我需要使用 PUT 方法通过 API 将一些受约束的实体发送到数据库。

这是提供这种方法的正确方式吗?

我问是因为我收到了 HTTP 500 错误代码响应。

我只能在Apache CXF官方文档中找到GET方法的例子;缺少 HTTP PUT、HTTP POST 等。

WebClient client = 
WebClient.create("http://MY_SERVER:9090/admission/services/processing");
Admission a = new Admission();
a.setCode("73935282");
:
:

Response r = client.path("/admission").put(a);
// Here I would like to get 201, but there is 500 :(
System.out.println("response: " + r.getStatus());

最佳答案

服务是否需要 XML 以外的内容类型,如 JSON? WebClient 的默认行为是假定 content-typeapplication/xml

这是相关的source code for WebClient.java :

protected Response doInvoke(String httpMethod, Object body, Class<?> responseClass, Type genericType) {

MultivaluedMap<String, String> headers = getHeaders();
if (body != null) {
if (headers.getFirst(HttpHeaders.CONTENT_TYPE) == null) {
headers.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_TYPE.toString());
}
} else {
headers.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.WILDCARD);
}
if (responseClass != null && headers.getFirst(HttpHeaders.ACCEPT) == null) {
headers.putSingle(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_TYPE.toString());
}
resetResponse();
return doChainedInvocation(httpMethod, headers, body, responseClass, genericType, null, null);
}

如果是这样,您可以使用 type()WebClient 上设置内容类型方法。例如,让客户端生成 JSON:

WebClient client = WebClient.create("http://MY_SERVER:9090/admission/services/processing");

client.type(MediaType.APPLICATION_JSON_TYPE);

Admission a = new Admission();
a.setCode("73935282");

Response r = client.path("/admission").put(a);

关于java - Apache CXF - 以 http 为中心的方法和从客户端到服务器的 PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976572/

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