我有一个用 jersey 和 JAXB 实现的客户端,我想发送一个 pojo 类将 Rest web 服务抛出到 .Net API 服务器端。当我将布尔属性设置为 true 并将其发送到服务器时,布尔值在返回的响应中变为 false(还检查了服务器端的日志) 我不知道发生了什么。有人可以帮我吗?
下面有一些代码,我在客户端使用它来调用 .Net 服务器端的 API
public String boolTest() {
try {
GetBoolTest getBoolTest = new GetBoolTest(true);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.addFilter(new LoggingFilter(System.out));
WebResource service = client.resource(getBaseURI());
ClientResponse response = null;
response = service.path("Test")
.header("Content-Type", "application/json; charset=UTF-8-")
.type(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class, getBoolTest);
GetBoolTestResponse getBoolTestResponse = null;
if (response.getStatus() == 200) {
getBoolTestResponse = response.getEntity(GetBoolTestResponse.class);
System.out.println(getBoolTestResponse.toString());
}else{
switch (response.getStatus()) {
case 400:
throw new Exception("BadRequestException:: malFormed message : " + response.getStatus());
case 401:
throw new Exception("NotAuthorizedException:: Authorized failure : " + response.getStatus());
case 403:
throw new Exception("forbiddenException:: not permitted to access : " + response.getStatus());
case 404:
throw new Exception("NotFoundException:: could not find resource : " + response.getStatus());
case 405:
throw new Exception("NotAllowedException:: HTTP method not supported : " + response.getStatus());
case 406:
throw new Exception("NotAcceptableException:: client media type not supported : " + response.getStatus());
case 415:
throw new Exception("NotSupportedException:: client posted media not supported : " + response.getStatus());
case 500:
throw new Exception("InternalServerErrorException:: General Server Error : " + response.getStatus());
case 503:
throw new Exception("ServiceUnAvailableException:: server is temporarily unavailable or busy : " + response.getStatus());
default:
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
}
return String.valueOf(getBoolTestResponse);
} catch (Exception e) {
e.printStackTrace();
return "1800";
}
}
Pojo类:
@XmlRootElement
public class GetBoolTest {
protected Boolean isValue;
public GetBoolTest() {
}
public GetBoolTest(Boolean isValue) {
this.isValue = isValue;
}
public Boolean getIsValue() {
return isValue;
}
@XmlElement
public void setIsValue(Boolean isValue) {
this.isValue = isValue;
}
}
响应类:
@XmlRootElement
public class GetBoolTestResponse {
protected boolean isValue;
public boolean getIsValue() {
return isValue;
}
public void setIsValue(boolean value) {
this.isValue = value;
}
@Override
public String toString() {
return "GetBoolTestResponse{" +
"getIsValue=" + isValue +
'}';
}
}
这里是日志输出:
1 * Client out-bound request
1 > POST http://**.**.***.***:***/api/Test/Test
1 > Content-Type: application/json
1 > Accept: application/json
{"value":"true"}
1 * Client in-bound response
1 < 200
1 < Transfer-Encoding: chunked
1 < Server: Microsoft-IIS/10.0
1 < Date: Mon, 24 Apr 2023 08:03:49 GMT
1 < X-Powered-By: ASP.NET
1 < Content-Type: application/json; charset=utf-8
1 <
{"value":false,"formatters":[],"contentTypes":[],"declaredType":null,"statusCode":200}
GetBoolTestResponse{value=false}
我试了很多方法,用post man测试没问题!也尝试了很多版本的 jersey 1.x 和 2.x 但仍然有这个问题。