简体   繁体   English

Spring Boot可以将Xml响应主体转换为Java对象,但是不能将Java对象转换为Xml请求主体

[英]Spring Boot can convert Xml response body to Java object, but can't convert Java object to Xml request body

When i am trying to send a request with the content-type application/vnd.vmware.vcloud.session+xml;version=5.6 spring boot gives the below exception: 当我尝试发送内容类型为application/vnd.vmware.vcloud.session+xml;version=5.6的请求时,Spring Boot给出以下异常:

Request processing failed; 请求处理失败; nested exception is org.springframework.web.client.RestClientException: 嵌套的异常是org.springframework.web.client.RestClientException:
Could not write request: no suitable HttpMessageConverter found for request type [com.vmware.vcloud.v1.SessionType] and content type [application/vnd.vmware.vcloud.session+xml;version=5.6] 无法写入请求:找不到适合请求类型[com.vmware.vcloud.v1.SessionType]和内容类型[application / vnd.vmware.vcloud.session + xml; version = 5.6]的HttpMessageConverter

But when i get a response from the server with the same content-type spring-boot successfully converts the response to SessionType object. 但是,当我从具有相同内容类型的服务器获得响应时,spring-boot成功将响应转换为SessionType对象。

Why spring boot gives the above exception when i am trying send a request? 为什么在我尝试发送请求时,Spring Boot会给出上述异常?

I found the problem. 我发现了问题。 Jaxb2RootElementHttpMessageConverter can read classes that are annotated with @XmlRootElement or @XmlType . Jaxb2RootElementHttpMessageConverter可以读取用@XmlRootElement@XmlType注释的类。

But it can write only classes that are annotated with @XmlRootElement . 但是它只能写以@XmlRootElement注释的类。 Since my classes are not annotated with @XmlRootElement its canWrite method returns false . 由于我的类未使用@XmlRootElement进行注释, @XmlRootElement其canWrite方法返回false

@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
    return (clazz.isAnnotationPresent(XmlRootElement.class) || clazz.isAnnotationPresent(XmlType.class)) &&
            canRead(mediaType);
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
    return (AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM