WebService学习之JAX-WS与Spring整合发布WebService
程序员文章站
2022-05-29 13:28:35
...
基于Spring IoC容器发布Web服务,能够大大降低WebService实现过程,也能够更好的与企业级应用进行整合,
本文將和大家介绍如何基于Spring和JAX-WS发布WebService。
1.新建Web服务接口和实现类
@WebService
public interface IUserWebService {
public User queryUserById(@WebParam(name = "userId") Integer userId);
public User addUser(User user);
}
@Component
@WebService(serviceName = "UserWebService", endpointInterface = "cn.webservice.IUserWebService")
public class UserWebServiceImpl implements IUserWebService {
public User queryUserById(Integer userId) {
User user=new User();
user.setUserId(userId);
user.setName("张三");
return user;
}
public User addUser(User user) {
user.setUserId(200);
return user;
}
}
2.在web.xml文件中添加spring的监听器配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/application*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.发布
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<context:component-scan base-package="cn"></context:component-scan>
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8080/services/"/>
</bean>
</beans>
本文將和大家介绍如何基于Spring和JAX-WS发布WebService。
1.新建Web服务接口和实现类
@WebService
public interface IUserWebService {
public User queryUserById(@WebParam(name = "userId") Integer userId);
public User addUser(User user);
}
@Component
@WebService(serviceName = "UserWebService", endpointInterface = "cn.webservice.IUserWebService")
public class UserWebServiceImpl implements IUserWebService {
public User queryUserById(Integer userId) {
User user=new User();
user.setUserId(userId);
user.setName("张三");
return user;
}
public User addUser(User user) {
user.setUserId(200);
return user;
}
}
2.在web.xml文件中添加spring的监听器配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/application*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.发布
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<context:component-scan base-package="cn"></context:component-scan>
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8080/services/"/>
</bean>
</beans>
上一篇: webapi日志记录(TXT存储)
下一篇: spring 线程池