栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

SpringBoot整合cxf发布webService

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合cxf发布webService


2. cxf的pom依赖

1
2     org.apache.cxf
3     cxf-spring-boot-starter-jaxws
4     3.2.4
5

3. 开始编写webService服务端
3.1 实体类entity

1 package com.example.demo.entity;
2
3 import java.io.Serializable;
4 /
5   @ClassName:User
6   @Description:测试实体
7   @author Jerry
8   @date:2018年4月10日下午3:57:38
9  */
10 public class User implements Serializable{
11
12     private static final long serialVersionUID = -3628469724795296287L;
13
14     private String userId;
15     private String userName;
16     private String email;
17     public String getUserId() {
18         return userId;
19     }
20     public void setUserId(String userId) {
21         this.userId = userId;
22     }
23     public String getUserName() {
24         return userName;
25     }
26     public void setUserName(String userName) {
27         this.userName = userName;
28     }
29     public String getEmail() {
30         return email;
31     }
32     public void setEmail(String email) {
33         this.email = email;
34     }
35     @Override
36     public String toString() {
37         return "User [userId=" + userId + ", userName=" + userName + ", email=" + email + "]";
38     }
39
40 }**

3.2 服务接口

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 Jerry

  • @date:2018年4月10日下午3:58:10*/
    //@WebService(targetNamespace=" http:="" service.demo.example.com")如果不添加的话,动态调用invoke的时候,会报找不到接口内的方法,具体原因未知.
    br/>@WebResult(name="String",targetNamespace="")
    public String getUserName(@WebParam(name = "userId") String userId);="">

    @WebResult(name=" string",targetnamespace="" )


    ="">
@WebResult(name=" string",targetnamespace="" )

3.3 服务接口的实现类

@WebResult(name=" string",targetnamespace="" )
  • @ClassName:UserServiceImpl

  • @Description:测试服务接口实现类

  • @author Jerry

  • @WebResult(name=" string",targetnamespace="" )


    @date:2018年4月10日下午3:58:58*/
    @WebService(serviceName=" userservice",="" 对外发布的服务名
    br/>@Component
    public class UserServiceImpl implements UserService{@Component
    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("Jerry@163.xom");
    br/>@Override
    public String getUserName(String userId) {
    return "userId为:" + userId;}
    @Override
    ="">="">="">
  • }


  • @Override


}****

="">
}
@Override

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;

@Configuration
public class CxfConfig {@Autowired

@Autowired
UserService userService;


@SuppressWarnings(" all")


="">*/
@SuppressWarnings(" all")
="">

站点服务

*/
@SuppressWarnings(" all")


br/>@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, userService);
endpoint.publish("/user");
return endpoint;
}="">

@Bean


@Bean


}

="">

@Bean

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;

public class CxfClient {

public static void main(String[] args) {
CxfClient.main1();
CxfClient.main2();
}


  • public static void main1() {


  • try {


  • // 接口地址


  • String address = "http://127.0.0.1/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();


  • }


  • }


  • public static void main2() {
    // 创建动态客户端
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://127.0.0.1/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();
    }
    }
    }

    6. 注意点.
    诚如之前所说,如果接口的注解上不加targetNamespace的话,动态调用的时候,会报如下的错误。


    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/244943.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号