1. 程式人生 > >idea使用springboot的webservice基於cxf

idea使用springboot的webservice基於cxf

獲取 列表 覆蓋 簡單 驗證 rom 處理 objects poi

SpringBoot整合CXF實例:

服務端構建

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>
/**
 * 作者信息實體
 * @author
oKong * */ @Data @Builder @AllArgsConstructor @NoArgsConstructor public class AuthorDto { String name; List<String> hobby; String birthday; String description; Sex sex; }
/**
 * 性別枚舉類
 * @author oKong
 *
 */
public enum Sex {

    MALE(
"male"), FEMALE("female"); String value; Sex(String value) { this.value = value; } public String value() { return value; } public static Sex fromValue(String v) { for (Sex c : Sex.values()) { if (c.value.equals(v)) {
return c; } } throw new IllegalArgumentException(v); } }
/**
 * 創建服務接口
 * @author oKong
 *
 */
@WebService(targetNamespace = WsConst.NAMESPACE_URI ,name = "authorPortType")
public interface AuthorService {

    /**
     * 根據名稱獲取作者信息
     * @author 作者:oKong
     */
    @WebMethod(operationName="getAuthorByName")
    AuthorDto getAuthor(@WebParam(name = "authorName") String name);

    /**
     * 獲取作者列表信息
     * @author oKong
     */
    @WebMethod
    List<AuthorDto> getAuthorList();
    
    /**
     * 返回字符串測試
     * @author oKong
     */
    String getAuthorString(@WebParam(name = "authorName")String name);
}
@WebService(
         targetNamespace = WsConst.NAMESPACE_URI, //wsdl命名空間 
         name = "authorPortType",                 //portType名稱 客戶端生成代碼時 為接口名稱
         serviceName = "authorService",           //服務name名稱
         portName = "authorPortName",             //port名稱
         endpointInterface = "cn.lqdev.learning.springboot.cxf.service.AuthorService")//指定發布webservcie的接口類,此類也需要接入@WebService註解
public class AuthorServiceImpl implements AuthorService{

    @Override
    public AuthorDto getAuthor(String name) {
        AuthorDto author = new AuthorDto();
        author.setBirthday("1990-01-23");
        author.setName("姓名:" + name);
        author.setSex(Sex.MALE);
        author.setHobby(Arrays.asList("電影","旅遊"));
        author.setDescription("描述:一枚趔趄的猿。現在時間:" + new Date().getTime());
        return author;
    }

    @Override
    public List<AuthorDto> getAuthorList() {
        List<AuthorDto> resultList = new ArrayList<>();
        AuthorDto author = new AuthorDto();
        author.setBirthday("1990-01-23");
        author.setName("姓名:oKong");
        author.setSex(Sex.MALE);
        author.setHobby(Arrays.asList("電影","旅遊"));
        author.setDescription("描述:一枚趔趄的猿。現在時間:" + new Date().getTime());
        resultList.add(author);
        resultList.add(author);
        return resultList;
    }

    @Override
    public String getAuthorString(String name) {
        AuthorDto author = getAuthor(name);
        return author.toString();
    }
}
@WebService(
         targetNamespace = WsConst.NAMESPACE_URI, //wsdl命名空間 
         name = "authorPortType",                 //portType名稱 客戶端生成代碼時 為接口名稱
         serviceName = "authorService",           //服務name名稱
         portName = "authorPortName",             //port名稱
         endpointInterface = "cn.lqdev.learning.springboot.cxf.service.AuthorService")//指定發布webservcie的接口類,此類也需要接入@WebService註解
/**
 * 常量類
 * @author oKong
 *
 */
public class WsConst {
    public static final String NAMESPACE_URI = "http://www.lqdev.cn/webservice";
}
/**
 * cxf配置類
 * @author oKong
 *
 */
@Configuration
public class CxfWebServiceConfig {
    
    //這裏需要註意  由於springmvc 的核心類 為DispatcherServlet
    //此處若不重命名此bean的話 原本的mvc就被覆蓋了。可查看配置類:DispatcherServletAutoConfiguration
    //一種方法是修改方法名稱 或者指定bean名稱 
    //這裏需要註意 若beanName命名不是 cxfServletRegistration 時,會創建兩個CXFServlet的。
    //具體可查看下自動配置類:Declaration org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration
    //也可以不設置此bean 直接通過配置項 cxf.path 來修改訪問路徑的
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //註冊servlet 攔截/ws 開頭的請求 不設置 默認為:/services/*
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }
    
    /**
     * 申明業務處理類 當然也可以直接 在實現類上標註 @Service
     * @author oKong
     */
    @Bean
    public AuthorService authorService() {
        return new AuthorServiceImpl();
    }
    
    /*
     * 非必要項
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        SpringBus springBus = new SpringBus();
        return springBus;
    }
    
    /*
     * 發布endpoint
     */
    @Bean
    public Endpoint endpoint(AuthorService authorService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), authorService);
        endpoint.publish("/author");//發布地址
        return endpoint;
    }
}

訪問:http://127.0.0.1:8080/ws/author?wsdl ,驗證是否發布成功。

客戶端調用:

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency> 

右鍵項目文件---webservice--

技術分享圖片

技術分享圖片

WsConst.java:

/**
 * 常量類
 * @author oKong
 *
 */
public class WsConst {
    public static final String NAMESPACE_URI = "http://www.lqdev.cn/webservice";
    public static final String SERVICE_ADDRESS= "http://127.0.0.1:8080/ws/author?wsdl";               //為服務端地址
}
/**
 * 配置類
 * 
 * @author oKong
 *
 */
@Configuration
public class CxfClientConfig {

    
    /**
     *  以接口代理方式進行調用 AuthorPortType接口
     */
    @Bean("cxfProxy")
    public AuthorPortType createAuthorPortTypeProxy() {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setServiceClass(AuthorPortType.class);
        jaxWsProxyFactoryBean.setAddress(WsConst.SERVICE_ADDRESS);//服務地址:http://127.0.0.1:8080/ws/autho

        return (AuthorPortType) jaxWsProxyFactoryBean.create();
    }
    
    /*
     * 采用動態工廠方式 不需要指定服務接口
     */
    @Bean
    public Client createDynamicClient() {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(WsConst.SERVICE_ADDRESS);
        return client;
    } 
}
/**
 * 調用示例
 * @author oKong
 *
 */
@RestController
@RequestMapping("/cxf")
public class DemoController {

    @Autowired
    Client client;
    
    @Autowired
    @Qualifier("cxfProxy")
    AuthorPortType authorPort;
    
    @GetMapping("/getauthorstring")
    public String getAuthorString(String authorName) {
        return authorPort.getAuthorString(authorName);
    }
    
    @GetMapping("/getauthor")
    public AuthorDto getAuthor(String authorName) {
        return authorPort.getAuthorByName(authorName);
    }
    
    @GetMapping("/getauthorlist")
    public List<AuthorDto> getAuthorList() {
        return authorPort.getAuthorList();
    }
    
    @GetMapping("/dynamic/{operation}")
    public Object getAuthorStringByDynamic(@PathVariable("operation")String operationName, String authorName) throws Exception {
        //這裏就簡單的判斷了 
        Object[] objects = null; 
//        client.getEndpoint().getBinding().getBindingInfo().getOperations()
        if ("getAuthorList".equalsIgnoreCase(operationName)) {
            objects = client.invoke(operationName);
        } else if ("getAuthorString".equalsIgnoreCase(operationName)) {
            objects = client.invoke(operationName, authorName);
        } else if ("getAuthorByName".equalsIgnoreCase(operationName)) {
            objects = client.invoke(operationName, authorName);
        } else {
            throw new RuntimeException("無效的調用方法");
        }
        return objects != null && objects.length > 0 ? objects[0] : "返回異常";
    }    
}

端口號配置:

server.port=8090


啟動應用,依次訪問。查看是否調用成功。

http://127.0.0.1:8090/cxf/getauthorstring?authorName=oKong

//為客戶端的地址

技術分享圖片

參考文獻:https://blog.csdn.net/xie19900123/article/details/83986482

idea使用springboot的webservice基於cxf