java - 如何配置 Spring-WS 以使用 JAXB Marshaller?

标签 java spring jaxb spring-ws endpoint

谢谢你到目前为止在这方面的帮助,我正在更新问题,因为我没有显示我需要的所有内容,并显示了建议的更改。肥皂输出仍然不是我想要的。

servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:sws="http://www.springframework.org/schema/web-services"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/web-services
                        http://www.springframework.org/schema/web-services/web-services-2.0.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/oxm 
                        http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"
                        >

<!--Enables @Endpoint and related Spring-WS annotations.-->
<sws:annotation-driven marshaller="marshaller" unmarshaller="marshaller"/> 

<bean id="weatherService"
    class="au.test.weather.ws.WeatherServiceImpl" />

<bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema" 
    p:xsd = "classpath:au/test/weather/ws/schemas/Temperature.xsd"/>

<oxm:jaxb2-marshaller id="marshaller" >
    <oxm:class-to-be-bound name="au.test.weather.ws.GetTemperaturesResponse"/> 
    <oxm:class-to-be-bound name="au.test.weather.ws.GetTemperaturesRequest"/>
    <oxm:class-to-be-bound name="au.test.weather.ws.schemas.Jaxb2Marshaller"/>  
</oxm:jaxb2-marshaller> 

<bean id="temperatureEndpoint"
    class="au.test.weather.ws.TemperatureMarshallingEndpoint">
    <property name="weatherService" ref="weatherService" />
</bean>

我的注释类是什么样的

@XmlRootElement(name = "GetTemperaturesRequest")
public class GetTemperaturesRequest {

    @XmlElement(required = true)
    protected String city;
    @XmlElement(required = true)
    @XmlSchemaType(name = "date")
    protected List<XMLGregorianCalendar> date;

    public String getCity() {
        return city;
    }

    public void setCity(String value) {
        this.city = value;
    }

    public List<XMLGregorianCalendar> getDate() {
        if (date == null) {
            date = new ArrayList<XMLGregorianCalendar>();
        }
        return this.date;
    }

    public void setDates(List<XMLGregorianCalendar> dates) {
this.date = dates;  
    }
}

端点

@Endpoint
public class TemperatureMarshallingEndpoint {

    private static final String namespaceUri = "http://test.au/schema/weather";
    public static final String request_local_name = "GetTemperaturesRequest";
    private WeatherService weatherService;

    public void setWeatherService(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

    @PayloadRoot(localPart = request_local_name, namespace = namespaceUri)
    @ResponsePayload
    public GetTemperaturesResponse getTemperature(@RequestPayload GetTemperaturesRequest request) throws JAXBException {
        List<GetTemperaturesResponse.TemperatureInfo> temperatures = weatherService.getTemperatures(request.getCity(), request.getDate());

        return new GetTemperaturesResponse(temperatures);
    }       
}

测试

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = {"classpath:servlet.xml"})

public class testOther {

    @Autowired
    private ApplicationContext applicationContext;                                           
    private MockWebServiceClient mockClient;

    @Before
    public void createClient() {
      mockClient = MockWebServiceClient.createClient(applicationContext);                  
    }

    @Test 
    public void TemperatureMarshallingEndpoint() throws Exception {
        Source requestPayload = new StringSource( 
        "<GetTemperaturesRequest xmlns='http://test.au/schema/weather'>" +
        "<city>Houston</city>" +
        "<date>2007-12-01</date>" + 
        "</GetTemperaturesRequest>");      

        Source responsePayload = new StringSource(
        "<GetTemperaturesResponse xmlns='http://test.au/schema/weather'>" +
        "<TemperatureInfo city='Houston' date='2007-12-01'><min>5.0</min><max>10.0</max><average>8.0</average></TemperatureInfo>" +
        "</GetTemperaturesResponse>");

        mockClient.sendRequest(withPayload(requestPayload)).  
        andExpect(payload(responsePayload));  
    }
} 

并且这个测试通过了,所以它一定是正确的,但是 soap 输出添加了 NS2 前缀

DEBUG: org.springframework.ws.server.MessageTracing.sent - 
Sent response 
[<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/><SOAP-ENV:Body>
<ns2:GetTemperaturesResponse xmlns:ns2="http://test.au/schema/weather">
    <ns2:TemperatureInfo city="Houston" date="2007-12-01">
        <ns2:min>5.0</ns2:min>
        <ns2:max>10.0</ns2:max>
        <ns2:average>8.0</ns2:average>
    </ns2:TemperatureInfo>
</ns2:GetTemperaturesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>] 
for request 
[<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/><SOAP-ENV:Body>
<GetTemperaturesRequest xmlns="http://test.au/schema/weather">
    <city>Houston</city>
    <date>2007-12-01</date></GetTemperaturesRequest>
</SOAP-ENV:Body></SOAP-ENV:Envelope>]

这个命名空间在哪里添加?

最佳答案

我建议这样做,基于最新的 Spring-WS,它更标准一些:

使用 oxm 命名空间定义你的 marhsaller:

<oxm:jaxb2-marshaller id="marshaller" >
    <oxm:class-to-be-bound name="...Your XMlRootElements.."/>
    <oxm:class-to-be-bound name="more.."/>  
</oxm:jaxb2-marshaller>

或者指定上下文路径:

<oxm:jaxb2-marshaller id="marshaller" contextPath="au.test.weather.ws"/>

删除对 GenericMarshallingMethodEndpointAdapterPayloadRootAnnotationMethodEndpointMapping 的引用,将两者替换为 sws 命名空间:

<sws:annotation-driven  />

或明确指定编码器/解码器:

<sws:annotation-driven marshaller="marshaller" unmarshaller="marshaller"/>

如果你的端点是用@EndPoint annotaion定义的,那么按照这些行:

@Endpoint
public class MyEndPoint{

    @PayloadRoot(namespace = "myns", localPart = "rootelement")
    @ResponsePayload
    public MyResponse myMethod(@RequestPayload MyRequest request)

它应该可以正常工作。此外,如果您的 MyRequest 类有一个 @XmlRootElement 注释,您甚至不需要指定编码器,它将使用内置的 MethodArgumentResolver 自动解析。

引用:http://static.springsource.org/spring-ws/sites/2.0/reference/html/server.html#server-endpoints

关于java - 如何配置 Spring-WS 以使用 JAXB Marshaller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532493/

相关文章:

java - JAXB继承+XMLAdapter(HashMap)

javascript - 将 for() 循环中的所有分号替换为 @ 符号

java - Android 适配器未更新

java - 使用 Spring MVC 进行 Hibernate 配置以进行测试

java - Spring boot RESTful - 三层架构

java - 通过 HTTP 发送旧版 JavaBean

java - 来自特定字节的 md5 文件校验和

java - 如何在 DockLayoutPanel 中显示和隐藏小部件?

java - Junit 和 spring websockets - 每次测试后我都会得到一个奇怪的异常

java - 在运行期间使用 JAXB 编写 X3D 格式的 XML