在使用Spring Boot创建Web服务时,如何在响应编组时删除null元素?

时间:2016-01-22 16:38:31

标签: spring web-services jaxb spring-boot spring-ws

我有一个web服务,它返回像<ns6:LastPayment/>这样的空元素。我相信它是由添加空的对象引起的,但我希望它们在发送到客户端之前被删除。这是由默认的编组处理的吗?

我的@Endpoint @ResponsePayload方法返回JAXBElement<ResponseObject> - 我是否应该假设Spring正在使用JAXB执行编组?

编辑:尝试添加有用的详细信息

的pom.xml

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.spring.platform</groupId>
            <artifactId>platform-bom</artifactId>
            <version>2.0.0.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-ws</artifactId>
    </dependency>
    <dependency>
        <!--allows configuration properties-->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

我有一个wsdl文件,我正在从中生成对象。

 <plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>3.1.4</version>
    <executions>
        <execution>
            <id>wsdl2java</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceRoot>${project.build.directory}/generated-sources</sourceRoot>
                <defaultOptions>
                    <extraargs>
                        <!--this allows "unwrapped" elements-->
                        <extraarg>-aer=true</extraarg>
                        <extraarg>-xjc-XReplacePrimitives</extraarg>
                        <extraarg>-xjc-XJsr303Annotations</extraarg>
                        <extraarg>-xjc-XJsr303Annotations:generateNotNullAnnotations=true</extraarg>
                        <extraarg>-xjc-XJsr303Annotations:JSR_349=true</extraarg>
                        <extraarg>-xjc-XJsr303Annotations:verbose=false</extraarg>
                    </extraargs>
                </defaultOptions>
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>${project.basedir}/src/main/resources/CARetriever.wsdl</wsdl>
                    </wsdlOption>
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.11.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.krasa</groupId>
            <artifactId>krasa-jaxb-tools</artifactId>
            <version>1.3</version>
        </dependency>
    </dependencies>
</plugin>

我的@ResponsePayload调用一个服务,该服务返回一个生成的ObjectFactory用作构造函数参数的对象。

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "RequestInfo")
@ResponsePayload
public JAXBElement<Response> getFnRetrieveInfo(@RequestPayload RequestInfo request)
        throws ResponseFault {
    return objectFactory.createResponse(myService.getReply(request));
}

我的服务获取数据并填充生成的对象以进行返回

GeneratedObj generatedObj = objectFactory.createGeneratedObj();
// this *might* be an empty string
generatedObj.setSomeProperty = myserviceResult.getSomeProperty();
// one possible state is that generatedObj is null
generatedReplyObj.addChild(generatedObj);

由于Response(包含Reply等)通过webservice返回,我假设Spring的默认marshaller正在进行处理。在这个例子中,我希望得到这样的结果:

<reply>
    <generatedobj>
        <someproperty>value</someproperty>
    </generatedobj>
</reply>

但是,如果该属性为空,则它仍然添加该对象,结果如下:

<reply>
    <generatedobj />
</reply>

我想避免这种情况(插入前没有经过大量测试)

其他编辑:添加回复定义

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FNRetrieveInfoReply", propOrder = {
    "quickResponse",
    "fnInfo",
    "caseParticipantHeader"
})
public class FNRetrieveInfoReply {

    @XmlElement(name = "QuickResponse", namespace = "urn:us:gov:hhs:acf:qikrsp", required = true)
    @NotNull
    @Valid
    protected QuickResponse quickResponse;
    @XmlElement(name = "FNInfo", namespace = "urn:us:gov:hhs:acf:fn", required = true)
    @NotNull
    @Valid
    protected FNInfo fnInfo;
    @XmlElement(name = "CaseParticipantHeader", namespace = "urn:us:gov:hhs:acf:chdr", required = true)
    @NotNull
    @Valid
    protected CaseParticipantHeader caseParticipantHeader;
}

(每个字段当然都有访问者)

进一步编辑:

好的,也许另一种检查方法可能与marshalling empty lists有关。但是,这里的区别在于对象的深度和类型。当JAXB包含另一个字段为null的对象时,它如何编组对象?我想我期待整个连锁店都被淘汰,但我认为它只是消除了最外层的链条。

0 个答案:

没有答案