项目使用springboot,首先附上项目结构
下面是pom中添加cxf支持:
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-spring-boot-starter-jaxwsartifactId>
<version>3.2.4version>
dependency>
package com.example.demo.entity;
import java.io.Serializable;
/**
* @ClassName:User
* @Description:测试实体
* @author Maple
* @date:2018年4月10日下午3:57:38
*/
public class User implements Serializable{
private static final long serialVersionUID = -3628469724795296287L;
private String userId;
private String userName;
private String email;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", email=" + email + "]";
}
}
package com.example.demo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import com.example.demo.entity.User;
/**
* @ClassName:UserService
* @Description:测试服务接口类
* include:两个测试方法
* @author Maple
* @date:2018年4月10日下午3:58:10
*/
@WebService
public interface UserService {
@WebMethod//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。
public User getUser(@WebParam(name = "userId") String userId);
@WebMethod
@WebResult(name="String",targetNamespace="")
public String getUserName(@WebParam(name = "userId") String userId);
}
package com.example.demo.service.impl;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
/**
* @ClassName:UserServiceImpl
* @Description:测试服务接口实现类
* @author Maple
* @date:2018年4月10日下午3:58:58
*/
@WebService(serviceName="UserService",//对外发布的服务名
targetNamespace="http://service.demo.example.com",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface="com.example.demo.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Component
public class UserServiceImpl implements UserService{
private Map userMap = new HashMap();
public UserServiceImpl() {
System.out.println("向实体类插入数据");
User user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test1");
user.setEmail("[email protected]");
userMap.put(user.getUserId(), user);
user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test2");
user.setEmail("[email protected]");
userMap.put(user.getUserId(), user);
user = new User();
user.setUserId(UUID.randomUUID().toString().replace("-", ""));
user.setUserName("test3");
user.setEmail("[email protected]");
userMap.put(user.getUserId(), user);
}
@Override
public String getUserName(String userId) {
return "userId为:" + userId;
}
@Override
public User getUser(String userId) {
System.out.println("userMap是:"+userMap);
return userMap.get(userId);
}
}
package com.example.demo.config;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.demo.service.UserService;
/**
* @ClassName:CxfConfig
* @Description:cxf发布webservice配置
* @author Maple
* @date:2018年4月10日下午4:12:24
*/
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
UserService userService;
/**
* 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
* 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl
* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl
* @return
*/
@SuppressWarnings("all")
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}
/** JAX-WS
* 站点服务
* **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, userService);
endpoint.publish("/user");
return endpoint;
}
}
package com.example.demo.client;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import com.example.demo.service.UserService;
/**
* @ClassName:CxfClient
* @Description:webservice客户端:
* 该类提供两种不同的方式来调用webservice服务
* 1:代理工厂方式
* 2:动态调用webservice
* @author Maple
* @date:2018年4月10日下午4:14:07
*/
public class CxfClient {
public static void main(String[] args) {
CxfClient.main2();
}
/**
* 1.代理类工厂的方式,需要拿到对方的接口地址
*/
public static void main1() {
try {
// 接口地址
String address = "http://127.0.0.1:8080/soap/user?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
// 创建一个代理接口实现
UserService us = (UserService) jaxWsProxyFactoryBean.create();
// 数据准备
String userId = "maple";
// 调用代理接口的方法调用并返回结果
String result = us.getUserName(userId);
System.out.println("返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 2:动态调用
*/
public static void main2() {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8080/soap/user?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("getUserName", "maple");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
当然也可以代码生成方式调用接口:
在你想生成代码的文件夹下shift+右击打开power Shell:
输入命令:wsimport http://127.0.0.1:8080/soap/user?wsdl
即可生成文件夹。
如果想要下载项目源码的话附上github地址:https://github.com/maplefix/maplefix