SpringBoot 集成Webservice

mac2024-04-01  26

引用依赖 

<!--Webservice--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency>

 

 

服务端发布服务

 

创建 CxfConfig类

package com.example.springmybatisshiro.common.config; import com.example.springmybatisshiro.modules.sys.service.IUserService; import com.example.springmybatisshiro.modules.sys.service.impl.UserServiceImpl; import com.example.springmybatisshiro.modules.webservice.IdemoService; import com.example.springmybatisshiro.modules.webservice.impl.DemoServiceImpl; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; @Configuration public class CxfConfig { @Bean public ServletRegistrationBean servletRegistrationBean(){ return new ServletRegistrationBean(new CXFServlet(),"/demo/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus(){ return new SpringBus(); } @Bean public IdemoService idemoService(){ return new DemoServiceImpl(); } @Bean public IUserService IUserService(){ return new UserServiceImpl(); } @Bean public Endpoint endpoint(){ EndpointImpl endpoint = new EndpointImpl(springBus(), idemoService()); endpoint.publish("/api"); return endpoint; } }

服务接口

package com.example.springmybatisshiro.modules.webservice; import com.example.springmybatisshiro.modules.sys.entity.User; import javax.jws.WebResult; import javax.jws.WebService; //@WebService(targetNamespace="http://webservice.modules.springmybatisshiro.example.com")如果不添加的话,动态调用invoke的时候,会报找不到接口内的方法,具体原因未知. @WebService(targetNamespace = "http://webservice.modules.springmybatisshiro.example.com") public interface IdemoService { public User getUser(String username); }

服务接口实现类

package com.example.springmybatisshiro.modules.webservice.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.springmybatisshiro.modules.sys.entity.User; import com.example.springmybatisshiro.modules.sys.mapper.UserMapper; import com.example.springmybatisshiro.modules.webservice.IdemoService; import org.springframework.beans.factory.annotation.Autowired; import javax.jws.WebService; @WebService(serviceName = "IdemoService",//对外发布的服务名 targetNamespace = "http://webservice.modules.springmybatisshiro.example.com",//指定你想要的名称空间,通常使用使用包名反转 endpointInterface = "com.example.springmybatisshiro.modules.webservice.IdemoService") //服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口 public class DemoServiceImpl implements IdemoService { @Autowired private UserMapper userMapper; @Override public User getUser(String username) { return userMapper.selectOne(new QueryWrapper<User>().eq("user_name",username)); } }

启动服务器就发布了 

 

好了这样就发布成功了

 

客户单调用 

第一总调用方式 (若你的接口 和实现类上面只写了@WebService 没有配置里面详细参数 就使用这种方式调用)

将文件生成到本地 (我使用的是idea)

将发布成功的wsdl地址帖进去 OK 就会生成到你指定的项目位置 package prefix

生成成功后就webservice下这些文件 

然后本地测试

package com.zhou.testclient; import com.zhou.testclient.modules.webservice.DemoServiceImplService; import com.zhou.testclient.modules.webservice.User; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class TestClientApplicationTests { @Test void contextLoads() { DemoServiceImplService implService = new DemoServiceImplService(); User user = implService.getDemoServiceImplPort().getUser("zhangsan"); System.out.println(user.getPassword()+""+user.getFullName()); } }

第二总调用方式

动态调用

public static void main(String[] args){ //调用webservice开放的接口 String url="http://localhost:8080/shiro/demo/api?wsdl"; JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(url); try { Object[] invoke = client.invoke("getUser", "zhangsan"); UserVo userVo = new UserVo(); BeanUtils.copyProperties(invoke[0],userVo); logger.info(userVo.getUserName()); }catch (java.lang.Exception e){ e.printStackTrace(); } }

若你的@webservice没有配置详细参数这样就会报如下错,

若你知道什么原因请留言,谢谢欢迎交流

 

最新回复(0)