Apache CXF + Spring: Creating a Simple Client

I started learning Apache CXF with Spring. First of all, I tried to create a simple client / server model.

Server side: service.HelloWorld.java

@WebService public interface HelloWorld { String sayHi(String text); } 

service.HelloWorldImpl.java

 @WebService(endpointInterface = "service.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { return "Hello, " + text; } } 

Client side: client.Client.java public class Client {

  public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"cxf-client-servlet.xml"}); HelloWorld client = (HelloWorld) context.getBean("client"); System.out.println(client.sayHi("Batman")); } } 

CXF-client-servlet.xml

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <bean id="client" class="service.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="service.HelloWorld"/> <property name="address" value="http://localhost:8080/services/HelloWorld"/> </bean> 

The problem is this: for the client to work, I had to add service.HelloWorld (package + interface) to the clients project. I heard that before using the service I need to create a stub. So it baffles me. So, what is the right approach and what is the best practice (maybe it is better to use some kind of contract-first approach or something like that)? Later I want to add WS-Security, so I need a strong background =)

Thanks in advance.

+5
source share
2 answers

If you are doing the first development of WS code, then it’s good to distribute the interface and pass it to the client. I believe that @WebService not needed (?) On the interface (for implementation only), so the client has no dependencies on this annotation.

Even if you run the first web services with code, you can still download the WSDL document that Apache CXF created for you and pass it to the client. With this approach (which is considered more mature, not to mention that it can be used on different platforms, for example .NET), the client should generate stubs (using a tool, for example wsdl2java ). This process will essentially create a very similar client interface automatically.

This is one of the reasons why so many people prefer contracted development - the same WSDL is used to create client windows and implement server-side WS. This limits the scope of (random) incompatibilities.

+2
source

You can use a simple spring configuration like this for the client side -

 <jaxws:client id="mywebServiceClient" serviceClass="com.saurzcode.TestService" address="http://saurzcode.com:8088/mockTestService"> <jaxws:binding> <soap:soapBinding version="1.2" mtomEnabled="true" /> </jaxws:binding> </jaxws:client> <cxf:bus> <cxf:outInterceptors> <bean class="com.saurzcode.ws.caller.SoapHeaderInterceptor" /> </cxf:outInterceptors> </cxf:bus> 

Ignore the interceptor if you do not need it.

More details in this post.

+4
source

Source: https://habr.com/ru/post/907034/


All Articles