WebService is a cross-programming language, remoting technology cross-platform operating system, widely used in the actual development, interface, system integration.
Server
- 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>
- 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>
- 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;
}
}
- 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>
-
Start a web service, test
the service started successfully, we can access in your browser:http://localhost:8081/webservice/hello?wsdl
wherehttp://localhost:8081
is our server access entry,/webservice
in ourweb.xml
configuration profilecxfServlet
correspondingservlet-mapping
,/hello
for the service to access theaddress
address, the last also need to add?wsdl
access 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.
Client -
Add cxf its dependencies
-
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. -
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>
- Write unit tests
used herein to test the spring unit, therefore, not only to importjunit
-dependent, but alsospring-test
dependent.
@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);
}
}
- 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.