首页 > 解决方案 > Spring JAXWS SimpleJaxWsServiceExporter 的最佳实践 - JaxWsPortProxyFactoryBean

问题描述

我正在从一本名为“Spring 4 in action”的书中学习 Spring。我实际上是在做网络服务部分;特别是“发布和使用 Web 服务”。

按照这本书,我已经能够以这种方式设置JAX-WS Web 服务

服务器端

SpittrServiceImpl: 公开 JAX-WS 服务的类

@Component
@WebService(serviceName="SpittrService")
public class SpittrServiceImpl {


    @Autowired
    SpitterRepository spitterRepository;

    @Autowired
    SpittleRepository spittleRepository;

    @WebMethod
    public String helloService() { return "Hello world"; }

    @WebMethod
    public Spitter showSpitterProfile(String username) {
        Spitter spitter = spitterRepository.findByUsername(username);
        spitter.setSpittleList(new ArrayList<>());
        System.out.println("SERVER SIDE - showSpitterProfile: " + spitter.toString());
        return spitter;
    }

    @WebMethod
    public Spittle findSpittleById(long spittleId) {
        Spittle spittle = spittleRepository.findById(spittleId);
        System.out.println("SERVER SIDE - findSpittleById: " + spittle.toString());
        return spittle;
    }
}

JAXWSConfig: 配置 JAXWS 服务的类

@Configuration
public class JAXWSConfig {

    @Bean
    public SimpleJaxWsServiceExporter jaxWsExporter() {
        SimpleJaxWsServiceExporter exporter = new SimpleJaxWsServiceExporter();
        exporter.setBaseAddress("http://localhost:8888/services/");
        return exporter;
    }
}

这将创建我最后报告的WSDL

客户端,我们有以下类:

ClientConfig: 将代理返回到服务器端创建的 WSDL 的类

@Configuration
@ComponentScan(basePackages = {"spittrClient", "spittr"})
public class ClientConfig {

    @Bean
    public JaxWsPortProxyFactoryBean spittrService() throws MalformedURLException {
        System.out.println("CREATING JAX-WS CONNECTION...");
        JaxWsPortProxyFactoryBean proxy = new JaxWsPortProxyFactoryBean();
        proxy.setWsdlDocumentUrl(new URL("http://localhost:8888/services/SpittrService?wsdl"));
        proxy.setServiceName("SpittrService");
        proxy.setPortName("SpittrServiceImplPort");
        proxy.setServiceInterface(SpittrService.class);
        proxy.setNamespaceUri("http://jaxws.remoting.spittr/");
        System.out.println("JAX-WS CONNECTION CREATED");
        return proxy;
    }
}

SpittrService: 暴露 WSDL 提供的服务方法的接口

@WebService(serviceName = "SpittrService")
public interface SpittrService {

    @WebMethod
    String helloService();

    @WebMethod
    Spitter showSpitterProfile(String username);

    @WebMethod
    Spittle findSpittleById(long spittleId);

}

Client: 通过 JAX-WS 调用 Server 提供的服务的类 @Component public class Client {

    public static void main(String[] args) {

        //Run the Client class directly from main method

        System.out.println("CLIENT MAIN");
        ApplicationContext ctx = new AnnotationConfigApplicationContext(ClientConfig.class);

        SpittrService spittrService = (SpittrService) ctx.getBean(SpittrService.class);
        Spitter spitter = spittrService.showSpitterProfile("PyroSandro");
        System.out.println("SPITTER CALLED BY JAX-WS: " + spitter.toString());

        Spittle spittle = spittrService.findSpittleById(111);
        System.out.println("SPITTLE CALLED BY JAX-WS: " + spittle.toString());

    }
}

一切正常,但我不喜欢实施。我想我应该SpittrService也在服务器端实现接口(在客户端),并且类SpittrServiceImpl必须实现它的方法。但是,如果我创建SpittrService界面并将注释移到那里@WebService@WebMethod从 中移出SpittrServiceImpl,它就会停止工作。

另一种选择是相同的注释@WebService@WebMethod放置在SpittrService接口和SpittrServiceImpl类上,但我不知道这是否是一个好习惯(但它有效)。

你能帮我理解我做错了什么吗?提前致谢

这里我报告一下Server Side创建的WSDL :

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://jaxws.remoting.spittr/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="SpittrService" targetNamespace="http://jaxws.remoting.spittr/">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://jaxws.remoting.spittr/" elementFormDefault="unqualified" targetNamespace="http://jaxws.remoting.spittr/" version="1.0">
            <xs:element name="findSpittleById" type="tns:findSpittleById"/>
            <xs:element name="findSpittleByIdResponse" type="tns:findSpittleByIdResponse"/>
            <xs:element name="helloService" type="tns:helloService"/>
            <xs:element name="helloServiceResponse" type="tns:helloServiceResponse"/>
            <xs:element name="showSpitterProfile" type="tns:showSpitterProfile"/>
            <xs:element name="showSpitterProfileResponse" type="tns:showSpitterProfileResponse"/>
            <xs:complexType name="helloService">
                <xs:sequence/>
            </xs:complexType>
            <xs:complexType name="helloServiceResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="showSpitterProfile">
                <xs:sequence>
                    <xs:element minOccurs="0" name="arg0" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="showSpitterProfileResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="tns:spitter"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="spitter">
                <xs:sequence>
                    <xs:element minOccurs="0" name="email" type="xs:string"/>
                    <xs:element minOccurs="0" name="firstName" type="xs:string"/>
                    <xs:element minOccurs="0" name="id" type="xs:long"/>
                    <xs:element minOccurs="0" name="lastName" type="xs:string"/>
                    <xs:element minOccurs="0" name="password" type="xs:string"/>
                    <xs:element minOccurs="0" name="status" type="xs:string"/>
                    <xs:element minOccurs="0" name="username" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="findSpittleById">
                <xs:sequence>
                    <xs:element name="arg0" type="xs:long"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="findSpittleByIdResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="tns:spittle"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="spittle">
                <xs:sequence>
                    <xs:element minOccurs="0" name="id" type="xs:long"/>
                    <xs:element minOccurs="0" name="latitude" type="xs:double"/>
                    <xs:element minOccurs="0" name="longitude" type="xs:double"/>
                    <xs:element minOccurs="0" name="message" type="xs:string"/>
                    <xs:element minOccurs="0" name="spitter" type="tns:spitter"/>
                    <xs:element minOccurs="0" name="time" type="xs:dateTime"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="helloService">
        <wsdl:part element="tns:helloService" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="showSpitterProfileResponse">
        <wsdl:part element="tns:showSpitterProfileResponse" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="showSpitterProfile">
        <wsdl:part element="tns:showSpitterProfile" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="findSpittleById">
        <wsdl:part element="tns:findSpittleById" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="findSpittleByIdResponse">
        <wsdl:part element="tns:findSpittleByIdResponse" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:message name="helloServiceResponse">
        <wsdl:part element="tns:helloServiceResponse" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="SpittrServiceImpl">
        <wsdl:operation name="helloService">
            <wsdl:input message="tns:helloService" name="helloService"> </wsdl:input>
            <wsdl:output message="tns:helloServiceResponse" name="helloServiceResponse"> </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="showSpitterProfile">
            <wsdl:input message="tns:showSpitterProfile" name="showSpitterProfile"> </wsdl:input>
            <wsdl:output message="tns:showSpitterProfileResponse" name="showSpitterProfileResponse"> </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="findSpittleById">
            <wsdl:input message="tns:findSpittleById" name="findSpittleById"> </wsdl:input>
            <wsdl:output message="tns:findSpittleByIdResponse" name="findSpittleByIdResponse"> </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="SpittrServiceSoapBinding" type="tns:SpittrServiceImpl">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="helloService">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="helloService">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="helloServiceResponse">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="showSpitterProfile">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="showSpitterProfile">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="showSpitterProfileResponse">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="findSpittleById">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="findSpittleById">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="findSpittleByIdResponse">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="SpittrService">
        <wsdl:port binding="tns:SpittrServiceSoapBinding" name="SpittrServiceImplPort">
            <soap:address location="http://localhost:8888/services/SpittrService"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

标签: javaspringweb-servicesjax-ws

解决方案


推荐阅读