天天看點

idea啟動webservice_idea使用springboot的webservice基于cxf

SpringBoot整合CXF執行個體:

服務端建構

org.apache.cxf

cxf-spring-boot-starter-jaxws

3.2.5

@Data

@Builder

@AllArgsConstructor

@NoArgsConstructorpublic classAuthorDto {

String name;

Listhobby;

String birthday;

String description;

Sex sex;

}

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);

}

}

@WebService(targetNamespace= WsConst.NAMESPACE_URI ,name = "authorPortType")public interfaceAuthorService {@WebMethod(operationName="getAuthorByName")

AuthorDto getAuthor(@WebParam(name= "authorName") String name);@WebMethod

ListgetAuthorList();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("描述:一枚趔趄的猿。現在時間:" + 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注解

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

}

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

@Bean("cxfServletRegistration")publicServletRegistrationBean dispatcherServlet() {//注冊servlet 攔截/ws 開頭的請求 不設定 預設為:/[email protected] authorService() {return newAuthorServiceImpl();

}@Bean(name=Bus.DEFAULT_BUS_ID)publicSpringBus springBus() {

SpringBus springBus= newSpringBus();returnspringBus;

}@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--

idea啟動webservice_idea使用springboot的webservice基于cxf
idea啟動webservice_idea使用springboot的webservice基于cxf

WsConst.java:

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"; //為服務端位址

}

@Configurationpublic classCxfClientConfig {@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;

}

}

@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