项目架构:SpringBoot+Orcal
前景: SpringBoot 项目中 对外公布接口,故采用 WebService ,在论坛上看了许多的代码示例,大体是分为以下几步:
1)引入jar包 ;
2)建立接口 interface ,并添加注解 @WebService 声明接口 以及 属性定义;
3)建立接口实现类,也要添加注解式声明 ;
4 )建立配置类 随项目启动 ;
经过自己实践,发现其实不需要那么繁琐,特总结如下,供己备用;
首选一张图说明 webService
如果是多个 webService 业务层 就注入 多个 endpoint ;
效果展示图:je
soupUI 添加 wsdl 地址,测试接口:
接下来 直接上 代码:
1.pom 文件引入jar 包依赖
org.springframework.boot
spring-boot-starter-web-services
org.apache.cxf
cxf-rt-frontend-jaxws
3.1.6
org.apache.cxf
cxf-rt-transports-http
3.1.6
2.定义webService类 实现业务层
注: webServie接口方法的返回值最好以 String JSON 字符串的形式返回,否则会出现数据格式不统一 难以维护;(使用原则:接口对外调用);返回值若是实体类对象或者是实体类对象数组、集合形式 ,返回的数据格式会直接 XML 节点-属性化;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import java.util.ArrayList;
import java.util.List;
public class MsgDealtWebService {
@Autowired
private WjzJcTsxxMapper jcTsxxMapper ;
@Autowired
private WjzJyTsxxMapper jyTsxxMapper ;
@WebMethod
public String getMsg(){
return "欢迎来到管理系统!" ;
}
@WebMethod
public String getMsg2(@WebParam String msg2){
return "欢迎" + msg2 + ",二次来到管理系统!" ;
}
@WebMethod
public String getWjzJcNoDealtJcMsgs(){
List list = new ArrayList<>();
list = jcTsxxMapper.selectAllNoCl();
return JSONObject.toJSonString(list) ;
}
}
3.建立 webService 启动配置类:
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 WebServiceCxfConfig {
//1.第一步配置访问服务器名称 如:发布服务名称 localhost:8080/webApi
@Bean
public ServletRegistrationBean disServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/webApi
@Bean
public MsgDealtWebService getmsgDealtWebService(){
return new MsgDealtWebService();
}
@Bean
public Endpoint msgDealt() {
EndpointImpl endpoint = new EndpointImpl(springBus(),getmsgDealtWebService());
endpoint.publish("/msgDealt"); // 接口访问地址
return endpoint;
}
}
4.项目启动,访问接口:http://localhost:port/项目名/webService暴露的服务器名 ,如http://localhost:8077/demo/webApi
5.SoupUI 调用地址测试 http://localhost:8077/demo/webApi/msgDealt?wsdl
感谢论坛上的各路大神



