【问题标题】:JAXB not able to convert value into BigDecimalJAXB 无法将值转换为 BigDecimal
【发布时间】:2018-11-02 20:52:00
【问题描述】:

在 Spring Boot 应用程序中,我使用 ma​​ven-jaxb2-plugin 从 WSDL 文件生成类:

<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>AUTODETECT</schemaLanguage>
                    <schemaDirectory>src/main/resources</schemaDirectory>
                    <schemaIncludes>
                        <include>*.wsdl</include>
                    </schemaIncludes>
                    <generatePackage>pl.pantuptus.app.integration</generatePackage>
                </configuration>
            </plugin>

WSDL 文件包含 sales 字段,定义为:

<s:element minOccurs="1" maxOccurs="1"
                        name="sales" type="s:decimal" />

ma​​ven-jaxb2-plugin 转换为生成类的 BigDecimal 属性:

@XmlElement(name = "sales", required = true)
protected BigDecimal sales;

我在配置组件中显式配置 JAXB Marshaller:

@Configuration
public class IntegrationConfig {
    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("pl.pantuptus.app.integration");
        return marshaller;
    }

    @Bean
    public MyClient myClient(Jaxb2Marshaller marshaller) {
        MyClient client = new MyClient();
        client.setDefaultUri("http://localhost:8080/ws");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}

我的问题是当我从客户端调用 WS 端点时:

getWebServiceTemplate().marshalSendAndReceive(url, request)

我收到一个具有 sales 属性的 null 值的对象。

我的猜测是 JAXB 无法正确解析此属性,因为它在响应中具有基于逗号的格式。

<sales>23 771,08</sales>

问题来了:我如何告诉 Jaxb2Marshaller(或任何其他 Marshaller 实现)如何将此类字符串转换为 BigDecimal?

【问题讨论】:

  • 我怀疑给定的销售 xml 值(“23 771,08”)对于底层架构是否正式有效。您是否验证过这个输入?
  • @Heri,谢谢你的提示。你是绝对正确的,打开验证后,ValidationEventHandler 开始揭示真相:.i.s.w.l.MyValidationEventHandler : LINKED EXCEPTION: java.lang.NumberFormatException

标签: java spring spring-boot jaxb


【解决方案1】:

IntegrationConfig 中将验证器添加到编组器后:

marshaller.setValidationEventHandler(new MyValidationEventHandler());

我发现没有针对 WSDL 验证 SOAP 文档(销售值中带有逗号):

i.s.w.l.MyValidationEventHandler:链接异常: java.lang.NumberFormatException

我认为唯一的解决方案是请求正确的 WSDL 文件或向服务提供者请求正确的 WS 响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2012-09-05
    • 2018-01-31
    相关资源
    最近更新 更多