您的位置:首页 > 编程语言 > Java开发

WebService学习总结(四)——使用spring+CXF开发WebService(服务端)

2018-03-30 15:03 561 查看
1.使用maven加载cxf包(版本为apache-cxf-3.1.15)<!--cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- cxf -->2.在web.xml中配置<!-- WebService:CXFServlet是要访问的webservice入口配置。 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>其中CXFServlet是要访问的webservice入口配置。
这个命名很重要,关系到你最后浏览器输入的路径
3.在spring的配置文件中配置<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<!-- 1.实现webservice服务的实现 -->
<bean id="userService" class="com.demo.service.impl.UserServiceImpl" />
<!-- 2.webservice接口配置 -->
<!-- implementor 指定 WebService 实现类, address 指定访问地址 -->
<jaxws:endpoint implementor="#userService" address="/login" publish="true" />
</beans>需要引入jax-ws的命名空间。并且引入两个xml配置文件,这两个文件在cxf的包里面,可以不用关心。 此处配置了一个bean,该bean是实现webservice服务的实现类。里面是具体的业务逻辑。jaxws:endpoint是配置webservice端地址的,implementor指向一个实现类,address是webservice的访问地址。结合web.xml里面的cxfServlet配置,访问webservice的地址应该为:http://IP:端口/项目名/service/login?wsdl
4.编写一个接口UserService@WebService
public interface UserService {
public User getUserById(int userId);

/**
* 用户登录Login
*/
@WebMethod
public void login(@WebParam(name="user") User user);@webservice注解说明这是一个webservice类,@WebParam是指明webservice接口方法的参数名称。类似于springmvc的@requestParam注解。此处如果不指明参数名称,webservice的wsdl文件里将使用arg0,arg1...代替,在写客户端代码请求webservice的时候,将引起困扰,不易编写代码。 

5.编写接口实现类
@Service("UserService")
@WebService(endpointInterface="com.demo.service.UserService")
public class UserServiceImpl implements UserService {

@Resource
private UserDao userDao;

public UserDao getUserDao() {
return userDao;
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/**
* 登录用户
*/
public void login(User user) {
getUserDao().login(user);

}
和上面接口不同的是,此处需要指明终端接口类的地址(包名+类名)

5.启动tomcat,访问:http://localhost:8081/ERPDEMO/service/login?wsdlERPDEMO是项目名,service是web.xml中配置的名字,login是开的接口

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.demo.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.demo.com/" name="UserServiceImplService" targetNamespace="http://impl.service.demo.com/">
<wsdl:import location="http://localhost:8081/ERPDEMO/service/login?wsdl=UserService.wsdl" namespace="http://service.demo.com/"></wsdl:import>
<wsdl:binding name="UserServiceImplServiceSoapBinding" type="ns1:UserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getPermissions">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getPermissions">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getPermissionsResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="selectByUserName">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="selectByUserName">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="selectByUserNameResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="login">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="login">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="loginResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="insert">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="insert">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="insertResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRoles">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getRoles">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getRolesResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getUserById">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getUserById">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUserByIdResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserServiceImplService">
<wsdl:port binding="tns:UserServiceImplServiceSoapBinding" name="UserServiceImplPort">
<soap:address location="http://localhost:8081/ERPDEMO/service/login"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>注意:1.  在类上添加@WebService注解,代表发布一个WebService服务
2. 通过EndPoint(端点服务)发布一个webService。Endpoint也是jdk提供的一个专门用于发布服务的类,它的publish方法接收两个参数,
  一个是本地的服务地址,二是提供服务的类。它位于javax.xml.ws.*包中。
3. Endpoint.publish(String address, Object implementor) 静态方法在给定地址处针对指定的实现者对象创建并发布端点
4. 默认public修饰的方法自动发布,静态方法不会公布
5. 如果希望某个方法不对外公开,可以在方法上添加@WebMethod(exclude=true),阻止对外公开。
6. 如果一个类上,被添加了@WebService注解,则必须此类至少有一个可以公开的方法,否则将会启动失败
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: