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("描述:一枚趔趄的猿。现在时间:" + 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就被覆盖了。可查看配置类: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

启动应用,依次访问。查看是否调用成功。

//为客户端的地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Idea写一个Spring Boot项目开发Web Service的Demo: 1. 创建Spring Boot项目 在Idea创建一个新的Spring Boot项目,选择WebSpring Web Services依赖项。 2. 创建一个Web Service 在src/main/java目录下创建一个包,命名为com.example.demo.webservice,然后在该包下创建一个类,命名为HelloWebService。 ``` package com.example.demo.webservice; import org.springframework.stereotype.Component; import javax.jws.WebMethod; import javax.jws.WebService; @Component @WebService(serviceName = "HelloWebService") public class HelloWebService { @WebMethod public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 这里我们使用了注解@WebService来标注这是一个Web Service,并使用@WebMethod标注了其一个方法。我们还使用了@Component注解来将此类标记为Spring Bean。 3. 配置Web Service 在application.properties文件添加以下配置: ``` # Web Service spring.webservices.path=/ws ``` 这将Web Service的路径设置为/ws。 4. 发布Web Service启动添加以下代码: ``` import com.example.demo.webservice.HelloWebService; import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PostConstruct; import javax.xml.ws.Endpoint; @SpringBootApplication public class DemoApplication { @Autowired private Bus bus; @Autowired private HelloWebService helloWebService; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @PostConstruct public void endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, helloWebService); endpoint.publish("/hello"); } } ``` 这里我们使用CXF框架来发布Web Service。在启动,我们注入了一个Bus对象和一个HelloWebService对象,并使用EndpointImpl将Web Service发布到路径/hello。 5. 测试Web Service 现在,我们可以使用任何Web Service客户端来测试我们的Web Service了。在浏览器访问http://localhost:8080/ws/hello?wsdl,你应该能够看到自动生成的WSDL描述文件。现在,你可以使用SoapUI等工具来测试我们的Web Service

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值