Java 如何配置SpringWS以使用JAXB封送器?

Java 如何配置SpringWS以使用JAXB封送器?,java,spring,jaxb,spring-ws,endpoint,Java,Spring,Jaxb,Spring Ws,Endpoint,谢谢你们在这方面的帮助,到目前为止,我正在更新问题,因为我并没有显示我需要的所有内容,并显示了建议的更改。soap输出仍然不是我想要的 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" x

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

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;  
    }
}
@XmlRootElement(name=“GetTemperaturesRequest”)
公共类获取温度请求{
@XmlElement(必需=true)
保护线城市;
@XmlElement(必需=true)
@XmlSchemaType(name=“date”)
受保护名单日期;
公共字符串getCity(){
回归城市;
}
公共void设置城市(字符串值){
这个城市=价值;
}
公共列表getDate(){
如果(日期==null){
日期=新的ArrayList();
}
返回此日期;
}
公共作废设置日期(列表日期){
this.date=日期;
}
}
终点

@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));  
    }
} 
@Endpoint
公共类温度MarshalingEndpoint{
私有静态最终字符串namespaceUri=”http://test.au/schema/weather";
公共静态最终字符串请求\u local\u name=“GetTemperaturesRequest”;
私人气象服务;
公共无效设置WeatherService(WeatherService WeatherService){
this.weatherService=weatherService;
}
@PayloadRoot(localPart=request\u local\u name,namespace=namespaceUri)
@回应书
public gettemperatureresponse getTemperature(@RequestPayload GetTemperaturesRequest request)抛出JAXBEException{
List temperatures=weatherService.getTemperatures(request.getCity(),request.getDate());
返回新的GetTemperaturesResponse(温度);
}       
}
测试

@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));  
    }
} 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={“classpath:servlet.xml”})
公共类testOther{
@自动连线
私有应用程序上下文应用程序上下文;
私有MockWebService客户端mockClient;
@以前
public void createClient(){
mockClient=MockWebServiceClient.createClient(applicationContext);
}
@试验
public void temperatureMarshalingEndpoint()引发异常{
Source requestPayload=新的StringSource(
"" +
“休斯顿”+
"2007-12-01" + 
"");      
Source responsePayload=新的StringSource(
"" +
"5.010.08.0" +
"");
mockClient.sendRequest(withPayload(requestPayload))。
andExpect(有效载荷(响应负载));
}
} 
这个测试通过了,所以它一定是正确的,但是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>]
DEBUG:org.springframework.ws.server.MessageTracing.sent-
发送响应
[
5
10
8
] 
请求
[
休斯敦
2007-12-01
]

这个名称空间是从哪里添加的?

我建议这样做,基于最新的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>
或显式指定封送拆收器/解封送拆收器:

<sws:annotation-driven marshaller="marshaller" unmarshaller="marshaller"/>
它应该会起作用。另外,如果MyRequest类有一个
@XmlRootElement
注释,您甚至不需要指定封送拆收器,它将使用内置的
MethodArgumentResolver
自动解析


参考资料:

您使用的是哪个版本的Spring WS?查看org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter的文档,从SpringWebServices2.0开始,它就被弃用,取而代之的是DefaultMethodEndpointAdapter和MarshallingPayloadMethodProcessor。也许你可以试试新的类。好吧,我不再使用折旧值了,我的spring ws是2.0.2版。我的新版本与类似,但我不知道如何应用它谢谢你的帮助,我已经用更多需要的信息更新了这个问题,并使用了oxm名称空间。你能解释一下sws之间的区别吗:带或不带属性marshaller和Unmarshaller的注释请看一下我的问题
@Endpoint
public class MyEndPoint{

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