Spring 启动 XML 更改根元素名称
Spring boot XML change root element name
我编写了一个 spring 启动应用程序来接受一个 Http get 请求并发送一个 XML 响应,因为 output.I 需要跟随 XML 作为通过 HTTP 的输出
<response xmlns="">
<userId>235</userId>
<amount>345.0</amount>
</response>
我的 DTO class 如下,
@XmlRootElement(name = "response")
public class CgPayment {
@XmlElement
private String userId;
@XmlElement
private double amount;
@XmlElement
public String getUserId() {
return userId;
}
@XmlElement
public void setUserId(String userId) {
this.userId = userId;
}
@XmlElement
public void setAmount(double amount) {
this.amount = amount;
}
@XmlElement
public double getAmount() {
return amount;
}
}
但我得到以下响应作为输出。
<CgPayment xmlns="">
<userId>235</userId>
<amount>345.0</amount>
</CgPayment>
如何更改根 element.The 响应类型为 APPLICATION_XML_VALUE
您是否尝试过将 class 名称更改为 Response ??我认为您的编组器正在从 class.
的名称中获取名称
我找到了这个(也许会有帮助)
If type() is JAXBElement.class , then namespace() and name() point to a
factory method with XmlElementDecl. The XML element name is the
element name from the factory method's XmlElementDecl annotation or if
an element from its substitution group (of which it is a head element)
has been substituted in the XML document, then the element name is
from the XmlElementDecl on the substituted element.If type() is not
JAXBElement.class, then the XML element name is the XML element name
statically associated with the type using the annotation
XmlRootElement on the type. If the type is not annotated with an
XmlElementDecl, then it is an error. If type() is not
JAXBElement.class, then this value must be "".
您使用的是 JAXB 特定注释,但 Jackson 负责整理您的响应。要使 JAXB 注释与 Jackson 一起使用,您必须在 pom.xml
中包含 jackson-module-jaxb-annotations
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
</dependency>
此外,您必须为您的配置注册 JaxbAnnotationModule
。我认为使用 Spring Boot 实现这一点的最简单方法是像这样注册一个 org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
类型的 Bean:
@Component
public class JacksonCustomizer implements Jackson2ObjectMapperBuilderCustomizer {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.modulesToInstall(new JaxbAnnotationModule());
}
或
@Bean
Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
return (mapperBuilder) -> mapperBuilder.modulesToInstall(new JaxbAnnotationModule());
}
您可以在 class 级别使用 @JacksonXmlRootElement(localName = "response")
。
我编写了一个 spring 启动应用程序来接受一个 Http get 请求并发送一个 XML 响应,因为 output.I 需要跟随 XML 作为通过 HTTP 的输出
<response xmlns="">
<userId>235</userId>
<amount>345.0</amount>
</response>
我的 DTO class 如下,
@XmlRootElement(name = "response")
public class CgPayment {
@XmlElement
private String userId;
@XmlElement
private double amount;
@XmlElement
public String getUserId() {
return userId;
}
@XmlElement
public void setUserId(String userId) {
this.userId = userId;
}
@XmlElement
public void setAmount(double amount) {
this.amount = amount;
}
@XmlElement
public double getAmount() {
return amount;
}
}
但我得到以下响应作为输出。
<CgPayment xmlns="">
<userId>235</userId>
<amount>345.0</amount>
</CgPayment>
如何更改根 element.The 响应类型为 APPLICATION_XML_VALUE
您是否尝试过将 class 名称更改为 Response ??我认为您的编组器正在从 class.
的名称中获取名称我找到了这个(也许会有帮助)
If type() is JAXBElement.class , then namespace() and name() point to a factory method with XmlElementDecl. The XML element name is the element name from the factory method's XmlElementDecl annotation or if an element from its substitution group (of which it is a head element) has been substituted in the XML document, then the element name is from the XmlElementDecl on the substituted element.If type() is not JAXBElement.class, then the XML element name is the XML element name statically associated with the type using the annotation XmlRootElement on the type. If the type is not annotated with an XmlElementDecl, then it is an error. If type() is not JAXBElement.class, then this value must be "".
您使用的是 JAXB 特定注释,但 Jackson 负责整理您的响应。要使 JAXB 注释与 Jackson 一起使用,您必须在 pom.xml
jackson-module-jaxb-annotations
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
</dependency>
此外,您必须为您的配置注册 JaxbAnnotationModule
。我认为使用 Spring Boot 实现这一点的最简单方法是像这样注册一个 org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
类型的 Bean:
@Component
public class JacksonCustomizer implements Jackson2ObjectMapperBuilderCustomizer {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.modulesToInstall(new JaxbAnnotationModule());
}
或
@Bean
Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
return (mapperBuilder) -> mapperBuilder.modulesToInstall(new JaxbAnnotationModule());
}
您可以在 class 级别使用 @JacksonXmlRootElement(localName = "response")
。