日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

idea启动webservice_idea使用springboot的webservice基于cxf

發布時間:2024/10/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 idea启动webservice_idea使用springboot的webservice基于cxf 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SpringBoot整合CXF實例:

服務端構建

org.apache.cxf

cxf-spring-boot-starter-jaxws

3.2.5

/*** 作者信息實體

*@authoroKong

**/@Data

@Builder

@AllArgsConstructor

@NoArgsConstructorpublic classAuthorDto {

String name;

Listhobby;

String birthday;

String description;

Sex sex;

}

/*** 性別枚舉類

*@authoroKong

**/

public enumSex {

MALE("male"),

FEMALE("female");

String value;

Sex(String value) {this.value =value;

}publicString value() {returnvalue;

}public staticSex fromValue(String v) {for(Sex c : Sex.values()) {if(c.value.equals(v)) {returnc;

}

}throw newIllegalArgumentException(v);

}

}

/*** 創建服務接口

*@authoroKong

**/@WebService(targetNamespace= WsConst.NAMESPACE_URI ,name = "authorPortType")public interfaceAuthorService {/*** 根據名稱獲取作者信息

*@author作者:oKong*/@WebMethod(operationName="getAuthorByName")

AuthorDto getAuthor(@WebParam(name= "authorName") String name);/*** 獲取作者列表信息

*@authoroKong*/@WebMethod

ListgetAuthorList();/*** 返回字符串測試

*@authoroKong*/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 implementsAuthorService{

@OverridepublicAuthorDto getAuthor(String name) {

AuthorDto author= newAuthorDto();

author.setBirthday("1990-01-23");

author.setName("姓名:" +name);

author.setSex(Sex.MALE);

author.setHobby(Arrays.asList("電影","旅游"));

author.setDescription("描述:一枚趔趄的猿。現在時間:" + newDate().getTime());returnauthor;

}

@Overridepublic ListgetAuthorList() {

List resultList = new ArrayList<>();

AuthorDto author= newAuthorDto();

author.setBirthday("1990-01-23");

author.setName("姓名:oKong");

author.setSex(Sex.MALE);

author.setHobby(Arrays.asList("電影","旅游"));

author.setDescription("描述:一枚趔趄的猿?,F在時間:" + newDate().getTime());

resultList.add(author);

resultList.add(author);returnresultList;

}

@OverridepublicString getAuthorString(String name) {

AuthorDto author=getAuthor(name);returnauthor.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注解

/*** 常量類

*@authoroKong

**/

public classWsConst {public static final String NAMESPACE_URI = "http://www.lqdev.cn/webservice";

}

/*** cxf配置類

*@authoroKong

**/@Configurationpublic classCxfWebServiceConfig {//這里需要注意 由于springmvc 的核心類 為DispatcherServlet//此處若不重命名此bean的話 原本的mvc就被覆蓋了??刹榭磁渲妙?#xff1a;DispatcherServletAutoConfiguration//一種方法是修改方法名稱 或者指定bean名稱//這里需要注意 若beanName命名不是 cxfServletRegistration 時,會創建兩個CXFServlet的。//具體可查看下自動配置類:Declaration org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration//也可以不設置此bean 直接通過配置項 cxf.path 來修改訪問路徑的

@Bean("cxfServletRegistration")publicServletRegistrationBean dispatcherServlet() {//注冊servlet 攔截/ws 開頭的請求 不設置 默認為:/services/*

return new ServletRegistrationBean(new CXFServlet(), "/ws/*");

}/*** 申明業務處理類 當然也可以直接 在實現類上標注 @Service

*@authoroKong*/@BeanpublicAuthorService authorService() {return newAuthorServiceImpl();

}/** 非必要項*/@Bean(name=Bus.DEFAULT_BUS_ID)publicSpringBus springBus() {

SpringBus springBus= newSpringBus();returnspringBus;

}/** 發布endpoint*/@BeanpublicEndpoint endpoint(AuthorService authorService) {

EndpointImpl endpoint= newEndpointImpl(springBus(), authorService);

endpoint.publish("/author");//發布地址

returnendpoint;

}

}

客戶端調用:

org.apache.cxf

cxf-spring-boot-starter-jaxws

3.2.5

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

WsConst.java:

/*** 常量類

*@authoroKong

**/

public classWsConst {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"; //為服務端地址

}

/*** 配置類

*

*@authoroKong

**/@Configurationpublic classCxfClientConfig {/*** 以接口代理方式進行調用 AuthorPortType接口*/@Bean("cxfProxy")publicAuthorPortType createAuthorPortTypeProxy() {

JaxWsProxyFactoryBean jaxWsProxyFactoryBean= newJaxWsProxyFactoryBean();

jaxWsProxyFactoryBean.setServiceClass(AuthorPortType.class);

jaxWsProxyFactoryBean.setAddress(WsConst.SERVICE_ADDRESS);//服務地址:http://127.0.0.1:8080/ws/autho

return(AuthorPortType) jaxWsProxyFactoryBean.create();

}/** 采用動態工廠方式 不需要指定服務接口*/@BeanpublicClient createDynamicClient() {

JaxWsDynamicClientFactory dcf=JaxWsDynamicClientFactory.newInstance();

Client client=dcf.createClient(WsConst.SERVICE_ADDRESS);returnclient;

}

}

/*** 調用示例

*@authoroKong

**/@RestController

@RequestMapping("/cxf")public classDemoController {

@Autowired

Client client;

@Autowired

@Qualifier("cxfProxy")

AuthorPortType authorPort;

@GetMapping("/getauthorstring")publicString getAuthorString(String authorName) {returnauthorPort.getAuthorString(authorName);

}

@GetMapping("/getauthor")publicAuthorDto getAuthor(String authorName) {returnauthorPort.getAuthorByName(authorName);

}

@GetMapping("/getauthorlist")public ListgetAuthorList() {returnauthorPort.getAuthorList();

}

@GetMapping("/dynamic/{operation}")public Object getAuthorStringByDynamic(@PathVariable("operation")String operationName, String authorName) throwsException {//這里就簡單的判斷了

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

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

//為客戶端的地址

總結

以上是生活随笔為你收集整理的idea启动webservice_idea使用springboot的webservice基于cxf的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。