在Spring MVC中使用SOAP WS

时间:2017-01-13 11:36:33

标签: spring-mvc soap spring-boot marshalling jaxb2

我正在尝试在spring启动应用程序中使用一些SOAP Web服务。我已经导入了ws的存根,我按照Web here的说法跟踪了WebServiceTemplate。不幸的是,在提出请求时我得到了一个例外:

  

2017-01-13 12:13:47.146 ERROR 1300 --- [nio-8080-exec-1] oaccC [。[。[/]。[dispatcherServlet]:servlet的[Servlet.service()[dispatcherServlet]在path []的上下文中抛出异常[请求处理失败;嵌套异常是org.springframework.oxm.MarshallingFailureException:JAXB编组异常;嵌套异常是javax.xml.bind.MarshalException     - 链接异常:   [com.sun.istack.SAXException2:no se ha podido canalizar el tipo“com.dashboard.dto.ComprobarSolicitud”como un elemento,porque le faltaunaanotación@ xmlRootElement]] with root cause

“ComprobarSolicitud”课程如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "comprobarSolicitud", propOrder = {
    "username",
    "passwd",
    "nif",
    "fechaDesde",
    "fechaHasta",
    "cantidad"
})
public class ComprobarSolicitud {

    protected String username;
    protected String passwd;
    protected String nif;
    protected String fechaDesde;
    protected String fechaHasta;
    protected int cantidad;

    // ...getters and setters

WebServiceGatewaySupport类:

public class PerClient extends WebServiceGatewaySupport {
    private static final Logger log = LoggerFactory.getLogger(PadronClient.class);

    public ComprobarSolicitudResponse comprobarSolicitudes(String pNif, LocalDate pFechaInicio, LocalDate pFechaFin){
    ComprobarSolicitud request = new ComprobarSolicitud();
    // .. set operations to request

    ComprobarSolicitudResponse response = (ComprobarSolicitudResponse) getWebServiceTemplate()
    .marshalSendAndReceive(
                    "https://ws.dir.com:8444/PerExterno/perExterno",
                    request,
                    new SoapActionCallback("http://service.ws.per.company.com/ExternalWS/comprobarSolicitudResponse"));

    return response;
}

}

配置类:

@Configuration
public class PerConfiguration {

    @Bean
    public Jaxb2Marshaller marshaller(){
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.dashboard.dto.per");
        return marshaller;
    }

    @Bean
    public PerClient padronClient(Jaxb2Marshaller marshaller){
        PerClient client = new PerClient();
        client.setDefaultUri("https://ws.dir.com:8444/PerExterno");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

}

我应该创建自定义编组程序吗?但是,怎么样?我找到this,据说如果缺少@XmlRootElement注释,我应该将它包装在JAXBElement的实例中。

谢谢

解<!/强>

异常是不言自明的,解决方案很简单,因为PerClient类需要修改如下:

public class PerClient extends WebServiceGatewaySupport {
    private static final Logger log = LoggerFactory.getLogger(PadronClient.class);

    public ComprobarSolicitudResponse comprobarSolicitudes(String pNif, LocalDate pFechaInicio, LocalDate pFechaFin){
    ComprobarSolicitud request = new ComprobarSolicitud();
    // .. set operations to request

    ObjectFactory of = new ObjectFactory();
    JAXBElement<ComprobarSolicitud> reqjaxb = of.createComprobarSolicitud(request);

    @SuppressWarnings("unchecked")
    JAXBElement<ComprobarSolicitudResponse> response = (ComprobarSolicitudResponse) getWebServiceTemplate()
    .marshalSendAndReceive(
                    "https://ws.dir.com:8444/PerExterno/perExterno",
                    reqjaxb ,
                    new SoapActionCallback("http://service.ws.per.company.com/ExternalWS/comprobarSolicitudResponse"));

    return response.getValue();
}

}

0 个答案:

没有答案