springboot集成webservice发布的服务有两种方式:
首先需要依赖:
org.apache.cxf cxf-rt-frontend-jaxws 3.1.12 // 第一个方式没有第三个依赖会报错 Cannot find any registered HttpDestinationFactory from the Bus. org.apache.cxf cxf-rt-transports-http 3.1.12 org.apache.cxf cxf-rt-transports-http-jetty 3.1.12
下面看代码:
package com.vrv.servers.service; import com.alibaba.fastjson.JSONArray; import com.vrv.servers.model.User; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; @WebService(targetNamespace = "http://service.hjc.web.com/wsdl") //@SOAPBinding(style = SOAPBinding.Style.RPC) public interface IUserService { // 获取登录信息 @WebMethod int getUserByName(@WebParam(name = "username") String username, @WebParam(name = "userpwd") String userpwd, @WebParam(name = "usertype") String usertype) throws Exception; // @WebMethod // User getUserInfo(String username, String userpwd, String usertype) throws Exception; }
// 实现层代码
package com.vrv.servers.impi; import com.vrv.servers.dao.UserDao; import com.vrv.servers.model.User; import com.vrv.servers.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import javax.jws.WebService; //@Service("userService") @WebService(endpointInterface = "com.vrv.servers.service.IUserService", targetNamespace = "http://service.hjc.web.com/wsdl",serviceName = "UserServiceImpi" ,portName = "dao") @Component public class UserServiceImpi implements IUserService { @Autowired UserDao userDao; public int getUserByName(String username, String userpwd, String usertype) throws Exception { int count = userDao.getUserCount(username, userpwd, usertype); System.out.println("count------>" + count); return count; } public User getUserInfo(String username, String userpwd, String usertype) throws Exception { // User user = userDao.getUser(username, userpwd, usertype); User user1 = new User(); user1.setLogin_type(usertype); user1.setStu_pwd(userpwd); user1.setStu_num(username); User user = this.userDao.getUser(user1); return user; } }
// javaBean
package com.vrv.servers.model; import java.io.Serializable; public class User implements Serializable { private int id; private String stu_num; private String stu_name; private String stu_pwd; private String login_type; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStu_num() { return stu_num; } public void setStu_num(String stu_num) { this.stu_num = stu_num; } public String getStu_name() { return stu_name; } public void setStu_name(String stu_name) { this.stu_name = stu_name; } public String getStu_pwd() { return stu_pwd; } public void setStu_pwd(String stu_pwd) { this.stu_pwd = stu_pwd; } public String getLogin_type() { return login_type; } public void setLogin_type(String login_type) { this.login_type = login_type; } public User() { } public User(int id, String stu_num, String stu_name, String stu_pwd, String login_type) { this.id = id; this.stu_num = stu_num; this.stu_name = stu_name; this.stu_pwd = stu_pwd; this.login_type = login_type; } @Override public String toString() { return "User{" + "id=" + id + ", stu_num='" + stu_num + '\'' + ", stu_name='" + stu_name + '\'' + ", stu_pwd='" + stu_pwd + '\'' + ", login_type='" + login_type + '\'' + '}'; } }
//第一种方法直接在boot启动文件中发布
package com.vrv.servers; import com.vrv.servers.impi.UserServiceImpi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.xml.ws.Endpoint; @SpringBootApplication public class ServersApplication { public static void main(String[] args) { // SpringApplication.run(ServersApplication.class, args); System.out.println("启动并发布webservice远程服务"); Endpoint.publish("http://127.0.0.1:11008/UserServiceImpi/dao?wsdl", new UserServiceImpi()); System.out.println("启动并发布webservice远程服务,服务发布成功...."); } }
// 访问 http://127.0.0.1:11008/UserServiceImpi/dao?wsdl
// 第二种方法就是使用配置类,不用在启动类中加Endpoint.publish,这种接口的端口号和服务器端口号是一致的。
package com.vrv.servers.config; import com.sun.org.apache.bcel.internal.generic.IUSHR; import com.vrv.servers.impi.UserServiceImpi; import com.vrv.servers.service.IUserService; 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.beans.factory.annotation.Autowired; 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 { @Autowired private Bus bus; @Autowired IUserService userService; /** * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问 * 此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/user?wsdl * 去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/user?wsdl * * @return */ @Bean public ServletRegistrationBean disServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/UserServiceImpi/*"); return servletRegistrationBean; } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } /** JAX-WS * 站点服务 * **/ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, userService); endpoint.publish("/dao"); return endpoint; } }
// 访问 http://127.0.0.1:10086/UserServiceImpi/dao?wsdl
出现的报错:
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.
这个问题是由于方法名造成的,改一下方法名就好了
发布结果