CXF study notes 2 (publishing WebService in WEB container)

Please reprint from the source: http://eksliang.iteye.com/blog/2265685

I. Overview

In "CXF Study Notes 1", the proxy factory JaxWsProxyFactoryBean is used to publish WebService, but this publishing has a defect, that is, it must run on the specified port. If the port is occupied, an error will be reported. If the current demand is: I have I want to deploy a WEB application in tomcat, and I also want to use port 8080 for the WebService published by my WEB application. What should I do?

A: This function can be accomplished by using the CXFServlet that comes with CXF.

 

2. Configure CXFServlet

Since it is CXFServlet, this servlet must be configured in web.xml, the code is as follows:

 

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- CXFServlet is used in Cxf to publish the same service as the current project port-->
<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>/cxf/*</url-pattern>
</servlet-mapping>
 

 

 By default, CXFServlet will load the cxf-servlet.xml configuration file under WEB-INF, but I have configured the contextConfigLocation here, so the CXF configuration loaded under the classpath also takes effect.

 

Third, the service interface and implementation class are as follows

interface:

 

import java.util.List;
import javax.jws.WebService;
import com.gosun.jws.daomain.Users;

/**
 * @author Ickes
 */
@WebService
public interface UserService {
	/**
	 * test return list
	 * @return
	 */
	public List<Users> geAlltUsers();
	/**
	 * Test return entity, and pass common parameters
	 * @param id
	 * @return
	 */
	public Users getUser(String id);
	
	/***
	 * Test incoming object
	 * @param user
	 */
	public void save(Users user);
	
	/**
	 * Test successor collection
	 * @param users
	 */
	public void saves(List<Users> users);
	
}
 Implementation class:

 

import java.util.List;
import javax.jws.WebService;
import com.gosun.jws.daomain.Users;
import com.gosun.jws.daomain.UsersFactory;

@WebService(serviceName = "cxfService",
			endpointInterface = "com.gosun.jws.cxf.UserService"
)
public class UserServiceImpl implements UserService {

	@Override
	public List<Users> geAlltUsers() {
		return UsersFactory.getUsers();
	}

	@Override
	public Users getUser(String id) {
		System.out.println(id);
		return UsersFactory.getUser();
	}

	@Override
	public void save(Users user) {
		System.out.println(user.toJson());
	}

	@Override
	public void saves(List<Users> users) {
		for (Users u : users) {
			System.out.println(u.toJson());
		}
	}
}

 Test used: UsersFactory tool class and Users entity class in: http://eksliang.iteye.com/blog/2265021 article 3.1 and 3.2 code

 

Fourth, configure the configuration file of CXF

Create a new applicationContext-cxf.xml file under the classpath directory with the following contents:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	
	<!-- CXFServlet will automatically publish com.gosun.jws.cxf.UserServiceImpl as WebService -->
	<jaxws:server id="cxfUserService" serviceClass="com.gosun.jws.cxf.UserService"
		address="/userService">
		<jaxws:serviceBean>
			<bean class="com.gosun.jws.cxf.UserServiceImpl" />
		</jaxws:serviceBean>
	</jaxws:server>

</beans>

 

 

5. Test

I use port 8080 to publish my WEB service, enter http://localhost:8080/jws/cxf in the browser, and the returned result is as follows.



 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326261132&siteId=291194637