简体   繁体   English

如何通过Spring Integration连接SOAP服务器和JSON客户端?

[英]How to connect SOAP server and JSON client through Spring Integration?

Good day. 美好的一天。 I am new to spring integration. 我是春季集成的新手。 I wrote a simple SOAP server, and I need to connect a client that communicates through JSON and a server that communicates via SOAP, but I've got confused in the technology that this framework provides. 我写了一个简单的SOAP服务器,我需要连接一个通过JSON进行通信的客户端和一个通过SOAP进行通信的服务器,但是我对该框架提供的技术感到困惑。 As I understand it there are JsonToObjectTransformer and ObjectToMapTransformer transformers. 据我了解,有JsonToObjectTransformer和ObjectToMapTransformer转换器。 As I understand it is necessary to transform the data before transmitting it to the controller. 据我了解,有必要在将数据传输到控制器之前对其进行转换。 Is it possible to do this with the help of transformers, or I can use other technologies in the spring integration. 是否可以在变压器的帮助下完成此操作,或者我可以在弹簧集成中使用其他技术。 And can this be done only with the help of DSL? 并且只能在DSL的帮助下完成吗?

Controller: 控制器:

@Endpoint
public class CityEndpoint {
    private static final String NAMESPACE_URI = "http://weather.com/senchenko";
    private CityRepository cityRepository;

    @Autowired
    public CityEndpoint(CityRepository cityRepository) {
        this.cityRepository = cityRepository;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCityRequest")
    @ResponsePayload
    public GetCityResponse getCityResponse(@RequestPayload GetCityRequest request){
        GetCityResponse response = new GetCityResponse();
        response.setCity(cityRepository.findCity(request.getName()));
        return response;
    }
}

Config: 配置:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    @Bean(name = "city")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema citySchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CityPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://weather.com/senchenko");
        wsdl11Definition.setSchema(citySchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema citySchema() {
        return new SimpleXsdSchema(new ClassPathResource("xsd/weather.xsd"));
    }

    @Bean
    @Transformer()
    JsonToObjectTransformer jsonToObjectTransformer() {
        return new JsonToObjectTransformer();
    }

    @Bean
    @Transformer()
    ObjectToMapTransformer objectToMapTransformer(){
        return new ObjectToMapTransformer();
    }
}

Addition 加成

I solved the problem with redirection to SOAP, but still do not know the best way to convert JSON into an SOAP Envelope and back. 我解决了重定向到SOAP的问题,但仍然不知道将JSON转换为SOAP信封并返回的最佳方法。

@Bean
    public IntegrationFlow httpProxyFlow() {
        return IntegrationFlows
                .from(Http.inboundGateway("/service"))
                .transform(t -> TEST_ENVOLOPE)
                .enrichHeaders(h -> h.header("Content-Type", "text/xml; charset=utf-8"))
                .handle(Http.outboundGateway("http://localhost:8080/ws")
                        .expectedResponseType(String.class))
                .transform(t -> TEST_RESPONSE)
                .get();
    }

Your question isn't clear or you are not fully familiar with technologies you need to work. 您的问题不清楚,或者您对需要工作的技术不完全熟悉。 The SOAP is fully about XML messages exchange. SOAP完全关于XML消息交换。 On the server side you have a specific MessageDispatcherServlet which converts an incoming HTTP request to the SOAP envelop fully in XML. 在服务器端,您有一个特定的MessageDispatcherServlet ,它将传入的HTTP请求完全转换为XML的SOAP信封。 There is just nothing about JSON at all. JSON完全没有。

Your CityEndpoint.getCityResponse() is triggered by the Spring WS Framework when an incoming SOAP request is unmarshalled from the XML into the domain model via JaxB according your XSD definition and generated model. 当传入的SOAP请求根据XML的XSD定义和生成的模型通过JaxB从XML编组到域模型中时,Spring WS Framework将触发CityEndpoint.getCityResponse() There is just nothing about Spring Integration at all. 根本没有关于Spring Integration的知识。

Your JsonToObjectTransformer and ObjectToMapTransformer just don't make any sense in this scenario. 在这种情况下,您的JsonToObjectTransformerObjectToMapTransformer毫无意义。 They are not involved in the SOAP request process. 它们不参与SOAP请求过程。

Sorry to disappoint you in my answer, but it even not clear by your question how that JSON client is going to call SOAP service when JSON and XML are fully different and not compatible protocols. 很抱歉让您失望,但您的问题甚至还不清楚,当JSON和XML完全不同且不兼容协议时,该JSON客户端将如何调用SOAP服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM