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

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: