WebService

1. JDK自带工具生成代码

1.1 代码

-s表示生成Java源代码

wsimport -s xx?wsdl

2. CXF

2.1 下载安装

下载地址: http://archive.apache.org/dist/cxf/
环境配置:

2.2 生成客户端代码

2.3 可能出现的问题

SpringBoot集成WebService

		<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starterjaxws</artifactId>
            <version>3.3.6</version>
        </dependency>

使用cxf-spring-boot-starterjaxws 3.3.6的版本springboot版本为2.2.5可以启动,2.3以上不行

  1. 服务端

webservice接口

@WebService(targetNamespace = "http://service.demo.example.com")
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);

实现类

@WebService(serviceName="UserService",//对外发布的服务名
        targetNamespace="http://service.demo.example.com",//指定你想要的名称空间,通常使用使用包名反转
        endpointInterface="com.example.webservice.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
@Service
public class UserServiceImpl implements UserService {

    private Map<String, User> userMap = new HashMap<String, User>();
    public UserServiceImpl() {
        System.out.println("向实体类插入数据");
        User user = new User();
        user.setUserId(UUID.randomUUID().toString().replace("-", ""));
        user.setUserName("test1");
        user.setEmail("Jerry@163.xom");
        userMap.put(user.getUserId(), user);

        user = new User();
        user.setUserId(UUID.randomUUID().toString().replace("-", ""));
        user.setUserName("test2");
        user.setEmail("Jerryfix@163.xom");
        userMap.put(user.getUserId(), user);

        user = new User();
        user.setUserId(UUID.randomUUID().toString().replace("-", ""));
        user.setUserName("test3");
        user.setEmail("Jerryfix@163.xom");
        userMap.put(user.getUserId(), user);
    }

    @Override
    public User getUser(String userId) {
        System.out.println("userMap是:"+userMap);
        return userMap.get(userId);
    }

    @Override
    public String getUserName(String userId) {
        return "userId为:" + userId;
    }

cxfconfig

springboot高版本已经cxf结合了,这个bean不需要写,可以直接配置yml

@Configuration
public class CxfConfig {
    @Autowired
    private Bus bus;
    @Autowired
    private 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   springboot高版本已经cxf结合了,这个bean不需要写,可以直接配置yml
     * @return
     */
    /*@SuppressWarnings("all")
    @Bean
    public ServletRegistrationBean disServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
    }*/

    /** JAX-WS
     * 站点服务
     * **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, userService);
        endpoint.publish("/user");
        return endpoint;
    }
}
cxf:
#此时wsdl地址为 localhost:8080/service/user?wsdl
  path: /service

  1. 客户端
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值