soap通信

pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.7.14
		 
	
	com.example
	forwardSoap
	0.0.1-SNAPSHOT
	forwardSoap
	Demo project for Spring Boot
	
		1.8
	
	
		
			cn.hutool
			hutool-all
			5.8.16
		

		
			javax.xml.bind
			jaxb-api
			2.3.1
		
		
		
			javax.activation
			activation
			1.1
		

		
			wsdl4j
			wsdl4j
		

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-web-services
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.projectlombok
			lombok
			1.18.16
			compile
		
	

	
		




















			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


countries.xsd文件



    
        
            
                
                
            
        
    

    
        
            
                
            
        
    

CountryEndpoint.java

package com.example.forwardSoap;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;


import javax.xml.soap.Node;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;


@Endpoint
public class CountryEndpoint {
    private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
//    public GetCountryResponse getCountry(@RequestPayload Source requestPayload) {
        // Convert Source to a DOM Document
//        Document document = (Document) ((DOMSource) requestPayload).getNode();

        String targetUrl = request.getTargetUrl();
        String envelopeRequest = request.getEnvelopeRequest();

        String result = HttpRequest.post(targetUrl)
                .header(Header.CONTENT_TYPE, "text/xml; charset=utf-8")
                .body(envelopeRequest)
                .execute().body();

        GetCountryResponse response = new GetCountryResponse();
        response.setEnvelopeResponse(result);

        return response;
    }


}

WebServiceConfig.java

package com.example.forwardSoap;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.server.EndpointAdapter;
import org.springframework.ws.server.endpoint.adapter.MessageEndpointAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@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 = "countries")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountriesPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
        wsdl11Definition.setSchema(countriesSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema countriesSchema() {
        return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
    }

}

ForwardSoapApplication.java

package com.example.forwardSoap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ForwardSoapApplication {
	public static void main(String[] args) {
		SpringApplication.run(ForwardSoapApplication.class, args);
	}
}

你可能感兴趣的:(java,spring,maven)