jaxwsportproxyfactorybean的java-spring jaxws最佳实践

wvyml7n5  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(253)

我正在从一本叫做“Spring4在行动”的书中学习spring。我实际上是在做webservice部分;尤其是“发布和消费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@component公共类客户端调用服务器提供的服务的类{

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` 上课,但我不知道这是否是一个好的做法(但它的工作)。
你能帮我理解我做错了什么吗?提前谢谢
这里我报告服务器端创建的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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题