spring integration cxf, easily write webService client, server

WebService is a cross-programming language, remoting technology cross-platform operating system, widely used in the actual development, interface, system integration.

Server

  1. List item

Add maven dependency
project-specific dependency in addition to spring, we need to add the following two dependencies.

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.3.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.3.5</version>
      <scope>compile</scope>
    </dependency>
  1. Configuring web.xml

In addition to conventional spring configuration, where the need to add cxfServlet configuration.

<!--  1.cxfServlet配置-->
  <servlet>
    <servlet-name>cXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>cXFServlet</servlet-name>
    <url-pattern>/webservice/*</url-pattern>
  </servlet-mapping>
<!--  2.配置spring容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
<!--    3.监听器-->
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  1. Write webService interface and implementation class
    IHelloService interface class
    Interfaces need to mark @webservice comment
@WebService
public interface IHelloService {

    /**
     *     对外发布服务接口的方法
     *      @param name
     */
    public String welcome(String name);
}

HelloServiceImpl interface class

public class HelloServiceImpl implements IHelloService {
    @Override
    public String welcome(String name) {
        return "welcome to webService "+name;
    }
}

  1. ApplicationContext.xml disposed
    in the namespace related to add cxf XSD, and configuration information corresponding to the service, including the service address, services etc.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">
    <cxf:server address="/hello">
        <jaxws:serviceBean>
            <bean class="service.impl.HelloServiceImpl"></bean>
        </jaxws:serviceBean>
    </cxf:server>
</beans>
  1. Start a web service, test
    the service started successfully, we can access in your browser: http://localhost:8081/webservice/hello?wsdlwhere http://localhost:8081is our server access entry, /webservicein our web.xmlconfiguration profile cxfServletcorresponding servlet-mapping, /hellofor the service to access the addressaddress, the last also need to add ?wsdlaccess wsdl specification.
    The picture shows the display ie browser, the Firefox browser I tested is a blank, consistent with the corresponding f12 content to the following content.
    Here Insert Picture DescriptionClient

  2. Add cxf its dependencies

  3. Introducing service interface class corresponding to the type
    here, we need to add the service to the end IHelloService HelloServiceImpl and client code.In addition, either the client or server, IHelloService interfaces are required labeling @webService annotations.

  4. Prepared applicationContext.xml client
    server interface defines the type of service, service address, corresponding thereto, the client needs to be configured the same service address, type of service interface.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd"
    <jaxws:client id="helloService" serviceClass="service.impl.HelloServiceImpl" address="http://localhost:8081/webservice/hello"></jaxws:client>
</beans>
  1. Write unit tests
    used herein to test the spring unit, therefore, not only to import junit-dependent, but also spring-testdependent.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {

    //注入对象
    @Resource
    private HelloServiceImpl helloService;
    @Test
    public void doClientRemote(){
        System.out.println(helloService);
        String content = helloService.welcome("Elaine");
        System.out.println(content);
    }

}
  1. View output
org.apache.cxf.jaxws.JaxWsClientProxy@45905bff
welcome to webService Elaine

Can be found, HelloServiceImpl objects we get a proxy object because the relevant interfaces in advance, is by nature jdk dynamic proxy implementation.
When viewing server output, there is a warning

org.apache.cxf.interceptor.Fault: Unexpected wrapper element {http://impl.service/}welcome found.   Expected {http://service/}welcome.

Well understood, we realize class, cxf hope we directly interface type in the proxy object acquisition unit test type. Replace with:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {

    //注入对象
    @Resource
    private IHelloService helloService;
    @Test
    public void doClientRemote(){
        System.out.println(helloService);
        String content = helloService.welcome("Elaine");
        System.out.println(content);
    }

}

Re-test, no warning, no error.

Published 98 original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/104868395