cxf spring 简单的搭建服务端
2015-11-20 12:03
447 查看
下载cxf官方包 lib文件夹里有一个 WHICH_JARS 按照这个说明导入相应的包
首先在spring application.xml里面 加上
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
>
<import resource="classpath:/cxf/cxf.xml"/>
<import resource="classpath:/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:/cxf/cxf-servlet.xml"/>
蓝色的地方为必须添加的地方
下面是cxf-servlet.xml
<?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" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:server id="userService" serviceClass="lc.item.cxf.UserService" address="/user">
<jaxws:serviceBean>
<bean class="lc.item.cxf.UserServiceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
其余2个xml都可以在jar里面找到然后 复制出来
cxf.xml 在cxf-core MATE-INF > cxf 下面
cxf-extension-soap.xml 在 cxf-rt-frontend-jaxws MATE-INF > cxf 下面
接下来是在web.xml里面配置
<!-- cxf -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
然后开始写接口类
package lc.item.cxf;
import javax.jws.WebParam;
import javax.jws.WebService;
import lc.item.sys.entity.User;
@WebService
public interface UserService {
public User getUser(@WebParam(name = "userId")Long userId);
}
package lc.item.cxf;
import javax.jws.WebMethod;
import javax.jws.WebService;
import lc.item.cxf.UserService;
import lc.item.sys.entity.User;
@WebService(endpointInterface="lc.item.cxf.UserService",serviceName="userService")
public class UserServiceImpl implements UserService{
public User getUser(Long userId) {
System.out.println(userId);
User user = new User();
user.setId(1001L);
user.setUsername("张三");
user.setEmail("dongwq@qq.com");
return user;
}
}
这2个类写好了以后 就可以访问 地址 http://127.0.0.1:8080/item/ws/
注:路劲里的ws 是在web.xml里面配置的
访问显示
下面继续访问 http://127.0.0.1:8080/item/ws/user?wsdl
路径里面的 user 是在cxf-servlet.xml 里面配置的那个 address
首先在spring application.xml里面 加上
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
>
<import resource="classpath:/cxf/cxf.xml"/>
<import resource="classpath:/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:/cxf/cxf-servlet.xml"/>
蓝色的地方为必须添加的地方
下面是cxf-servlet.xml
<?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" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:server id="userService" serviceClass="lc.item.cxf.UserService" address="/user">
<jaxws:serviceBean>
<bean class="lc.item.cxf.UserServiceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
其余2个xml都可以在jar里面找到然后 复制出来
cxf.xml 在cxf-core MATE-INF > cxf 下面
cxf-extension-soap.xml 在 cxf-rt-frontend-jaxws MATE-INF > cxf 下面
接下来是在web.xml里面配置
<!-- cxf -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
然后开始写接口类
package lc.item.cxf;
import javax.jws.WebParam;
import javax.jws.WebService;
import lc.item.sys.entity.User;
@WebService
public interface UserService {
public User getUser(@WebParam(name = "userId")Long userId);
}
package lc.item.cxf;
import javax.jws.WebMethod;
import javax.jws.WebService;
import lc.item.cxf.UserService;
import lc.item.sys.entity.User;
@WebService(endpointInterface="lc.item.cxf.UserService",serviceName="userService")
public class UserServiceImpl implements UserService{
public User getUser(Long userId) {
System.out.println(userId);
User user = new User();
user.setId(1001L);
user.setUsername("张三");
user.setEmail("dongwq@qq.com");
return user;
}
}
这2个类写好了以后 就可以访问 地址 http://127.0.0.1:8080/item/ws/
注:路劲里的ws 是在web.xml里面配置的
访问显示
下面继续访问 http://127.0.0.1:8080/item/ws/user?wsdl
路径里面的 user 是在cxf-servlet.xml 里面配置的那个 address
相关文章推荐
- javaweb学习总结(二十八)——JSTL标签库之核心标签
- 【转】javax.net.ssl.SSLHandshakeException(Cas导入证书)
- JAVA 泛型详解二
- JAVA 泛型详解一
- java技巧--提高代码运行效率
- 关于 Java 数组的 12 个最佳方法
- Java中,你遇到的错误 :程序包不存在
- Java内存分配的原理
- 【日志】Java日志工具(common-logging/log4j/slf4j)在常用框架(Struts2/Spring/Hibernate/Mybatis)中的灵活运用
- maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项
- 【Eclipse】--Eclipse加载现有项目
- java 内部类与外部类实例化顺序
- Eclipse新建工程编译R cannot be resolved to a variable问题
- Eclipse新建工程编译R cannot be resolved to a variable问题
- Eclipse如何生成jar包
- Eclipse如何生成jar包
- java.lang.SecurityException: The jurisdiction policy files are not signed by a trusted signer!
- java.security.InvalidKeyException: Illegal key size or default parameters
- 自定义注解实现spring 方法拦截用于日志,等处理
- r二2。1