如何春季-WS配置为使用JAXB的Marshaller?(How to configure Spri

2019-07-30 11:58发布

三江源这个到目前为止,我更新的问题的帮助,因为我并没有显示所有我需要,以改变recomended所示。 肥皂输出仍然不是我想要的。

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));  
    }
} 

这个测试通过,然而这样的肥皂产量增加了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>]

哪里该命名空间被添加?

Answer 1:

我建议做这种方式,这是基于最新的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>

或者指定的contextPath:

<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



文章来源: How to configure Spring-WS to use JAXB Marshaller?