HTTP请求正文示例
<Name>Cátia</Name>
HTTP响应正文示例
<Name>C�tia</Name>
这是请求类
@JacksonXmlRootElement(localName = "Root")
public class Request {
@JacksonXmlProperty(isAttribute = true, localName = "Id")
private String id;
@JacksonXmlProperty(isAttribute = true, localName = "Name")
private String name;
}
这是HTTP请求
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/xml;charset=UTF-8");
HttpEntity<> httpEntity = new HttpEntity<>(request, httpHeaders);
responseEntity = restTemplate
.exchange("URL", HttpMethod.POST, httpEntity, Response.class);
这是Response类
@JsonDeserialize(using = ResponseDeserializer.class)
public class Response {
@JacksonXmlProperty(localName = "NAME")
public String name;
}
这是ResponseDeserializer类
public class ResponseDeserializer extends JsonDeserializer<Response> {
@Override
public Response deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
ObjectCodec objectCodec = p.getCodec();
JsonNode jsonNode = objectCodec.readTree(p);
return JsonUtils.getObjectMapper().readValue(JsonUtils.getObjectMapper().writeValueAsString(jsonNode),
Response.class);
}
}
这是个例外
JsonNode jsonNode = objectCodec.readTree(p);
readTree方法抛出'com.fasterxml.jackson.core.JsonParseException' 例外。无效的UTF-8中间字节0x74(在char#2480,字节#1371处)
答案 0 :(得分:0)
通过添加isUTF8Support = true作为HTTP URL参数来解决。