`

webservice之cxf的服务提供方的使用方式

 
阅读更多
一、直接使用标注的方式
1、在pom文件中添加必需的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xue</groupId>
	<artifactId>CxfWS</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name />
	<description />
	<dependencies>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>2.7.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>2.7.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.1.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>2.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-http</artifactId>
			<version>8.1.7.v20120910</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-server</artifactId>
			<version>8.1.8.v20121106</version>
		</dependency>
	</dependencies>
	<build>
		<sourceDirectory>${basedir}/src</sourceDirectory>
		<outputDirectory>${basedir}/WebContent/WEB-INF/classes</outputDirectory>
		<resources>
			<resource>
				<directory>${basedir}/src</directory>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<webappDirectory>${basedir}/WebContent</webappDirectory>
					<warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.cxf</groupId>
				<artifactId>cxf-codegen-plugin</artifactId>
				<version>${cxf.version}</version>
				<executions>
					<execution>
						<id>generate-sources</id>
						<phase>generate-sources</phase>
						<configuration>
							<wsdlOptions>
								<wsdlOption>
									<wsdl>src/main/resources/OrderProcess.wsdl</wsdl>
								</wsdlOption>
							</wsdlOptions>
						</configuration>
						<goals>
							<goal>wsdl2java</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

2、定义接口
@WebService
public interface HelloWorld {
 
    String sayHi(String text);
 
 
    /* Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
     * support interfaces directly.  Special XmlAdapter classes need to
     * be written to handle them
     */
    String sayHiToUser(User user);
 
 
    /* Map passing
     * JAXB also does not support Maps.  It handles Lists great, but Maps are
     * not supported directly.  They also require use of a XmlAdapter to map
     * the maps into beans that JAXB can use.
     */
    @XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
    Map<Integer, User> getUsers();
}

为了保证参数在xml文件中正确的命名,应如下添加参数命名
@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name="text") String text);
}

3、完成接口实现类
package demo.hw.server;
 
import java.util.LinkedHashMap;
import java.util.Map;
 
import javax.jws.WebService;
 
@WebService(endpointInterface = "demo.hw.server.HelloWorld",
            serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
 
 
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
 
    public String sayHiToUser(User user) {
        System.out.println("sayHiToUser called");
        users.put(users.size() + 1, user);
        return "Hello "  + user.getName();
    }
 
    public Map<Integer, User> getUsers() {
        System.out.println("getUsers called");
        return users;
    }
 
}

4、发布服务接口

package demo.hw.server;

import javax.xml.ws.Endpoint;

public class Server {

    protected Server() throws Exception {
        // START SNIPPET: publish
        System.out.println("Starting Server");
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:9000/helloWorld";
        Endpoint.publish(address, implementor);
        // END SNIPPET: publish
    }

    public static void main(String args[]) throws Exception {
        new Server();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

另外,可使用如下的代码,将会有更多的控制能力,如添加日志拦截
HelloWorldImpl implementor = new HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/helloWorld");
svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(new LoggingInInterceptor());
svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
svrFactory.create();

在浏览器中访问 http://localhost:9000/helloWorld?wsdl,可以查看此服务的wsdl
二、使用spring来实现服务提提供者
前三步与上一种方式一样,只有发布方式不同
在WEB-INF下新建cxf-servlet.xml文件,内容如下
<?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:jaxws="http://cxf.apache.org/jaxws"
	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">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl"
		address="/HelloWorld" />

</beans>


如果要引用spring管理的bean,可以通过如下方式:
<bean id="hello" class="demo.spring.service.HelloWorldImpl" />
 
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />

配置servlet信息,web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/cxf-servlet.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<servlet>

		<servlet-name>CXFServlet</servlet-name>

		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

		<load-on-startup>1</load-on-startup>

	</servlet>



	<servlet-mapping>

		<servlet-name>CXFServlet</servlet-name>

		<url-pattern>/*</url-pattern>

	</servlet-mapping>
</web-app>


部署到tomcat服务器,输入:http://localhost:8080/<web-app-name>/ HelloWorld?wsdl,将显示这个web service的wsdl.

注意:如果web.xml配置<servlet-name>CXFServlet</servlet-name>

        <url-pattern>/ws/*</url-pattern>

则访问地址为:http://localhost:8080/<web-app-name>/ws/ HelloWorld?wsdl
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics