使用CXF开发WebService服务器端接口

CXF作为java领域主流的WebService实现框架,Java程序员有必要掌握它。

CXF主页:http://cxf.apache.org/

简介:百度百科

1.首先建一个Maven的项目,项目结构:


pom.xml文件引入:

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-core</artifactId>
			<version>3.1.5</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.5</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.1.5</version>
		</dependency>
</dependencies>

2.创建一个interface service

package com.milosun.webservice.service;

import javax.jws.WebService;

@WebService
public interface HelloWorldService {
	public String say(String name);
}

3.创建一个 implements class

package com.milosun.webservice.service.impl;

import javax.jws.WebService;

import com.milosun.webservice.service.HelloWorldService;


@WebService
public class HelloWorldServiceImpl implements HelloWorldService {

	public String say(String name) {
		
		return "Hello:"+name;
	}

}

4.创建一个server class

package com.milosun.webservice.server;

import javax.xml.ws.Endpoint;

import com.milosun.webservice.service.HelloWorldService;
import com.milosun.webservice.service.impl.HelloWorldServiceImpl;

public class Server {

	public static void main(String[] args) {
		System.out.println("web service start!");
		HelloWorldService implementor = new HelloWorldServiceImpl();
		String address = "http://192.168.1.100/helloWorld";
		Endpoint.publish(address, implementor);
		System.out.println("web service end!");
	}
}


或者:

package com.milosun.webservice.server;


import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.milosun.webservice.service.HelloWorldService;
import com.milosun.webservice.service.impl.HelloWorldServiceImpl;

public class Server {

	public static void main(String[] args) {
		System.out.println("web service start!");
		HelloWorldService implementor = new HelloWorldServiceImpl();
		String address = "http://192.168.1.100/helloWorld";
		
		//Endpoint.publish(address, implementor); //JDK 实现
		
        JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
        factoryBean.setAddress(address); // 设置暴露地址
        factoryBean.setServiceClass(HelloWorldService.class); // 接口类
        factoryBean.setServiceBean(implementor); // 设置实现类
        factoryBean.create();      
		System.out.println("web service started!");
	}
}

结果是一样的效果,只不过调用的方式不同。


5.测试结果:

console print:

web service start!
十二月 20, 2017 1:04:01 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://impl.service.webservice.milosun.com/}HelloWorldServiceImplService from class com.milosun.webservice.service.HelloWorldService
十二月 20, 2017 1:04:02 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://192.168.1.100:80/helloWorld
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
web service end!

browser 访问:http://192.168.1.103/helloWorld?wsdl



结论:这样一个简单的ws server 就创建完成了,下一章我们开始编写一个客户端来调用者个接口。


猜你喜欢

转载自blog.csdn.net/qq_23315711/article/details/78852043