关闭

cxf webservice执行数据库操作

标签: cxf数据库操作
489人阅读 评论(0) 收藏 举报
分类:

cxf webservice执行数据库操作,做个笔记,以免以后忘了。

IUserService.java

public interface IUserService {
    public User getUser(User user);
    public void addUser(User user);
    public User getUserByUsername(String username);
}

UserServiceImpl.java

public class UserServiceImpl implements IUserService {

    @Resource(name="userDao")
    public UserDao userDao;

    public User getUser(User user) {
        return userDao.getUser(user);
    }

    public void addUser(User user) {
        userDao.saveOrUpdateEntity(user);
    }

    public User getUserByUsername(String username) {
        return userDao.getUserByUsername(username);
    }
}

接下来写webservice的接口。从webservice的接口调用你项目里的非webservice的接口。
IUserWebService.java

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import model.User;

@WebService
@SOAPBinding(style=Style.RPC)
public interface IUserWebService {
    public User getUser(@WebParam(name="user")User user);

    public void addUser(@WebParam(name="user")User user);

    public User getUserByUsername(@WebParam(name="username")String username);
}

接口的实现方法:
UserWebService.java

import iservice.IUserService;
import iservice.IUserWebService;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import model.User;
@WebService
@SOAPBinding(style = Style.RPC)
public class UserWebService implements IUserWebService {
    @Resource
    private IUserService userService;

    public User getUser(User user) {
        System.out.println("go into webservice...");
        User u = userService.getUser(user);
        return u;
    }

    public void addUser(User user) {
        userService.addUser(user);
    }

    public User getUserByUsername(String username) {
        return userService.getUserByUsername(username);
    }
}

配置文件applicationContext-cxf.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"
    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-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="userServiceBean" class="cxf.UserWebService" />

    <!-- 
    <bean id="inMessageInterceptor" class="com.hoo.interceptor.MessageInterceptor">
        <constructor-arg value="receive" />
    </bean>
     -->
    <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor">
    </bean>

    <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
    <!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
    <jaxws:server id="userWebService" serviceClass="iservice.IUserWebService" address="/UserWebS">
        <jaxws:serviceBean>
            <!-- 要暴露的 bean 的引用 -->
            <ref bean="userServiceBean" />
        </jaxws:serviceBean>
    </jaxws:server>

</beans>

在web.xml中添加:

    <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

在web容器中发布后的webservice的访问路径:
http://localhost:8081/SSH2/ws/UserWebS?wsdl

然后创建客户端:
可以通过命令生成客户端代码:
需要用到cxf的的bat生成接口的类,把生成的类拷到自己工程中,然后在自己的客户端调用。
C:\Users\IBM_ADMIN>C:\JavaWeb\apache-cxf-3.0.4\bin\wsdl2java.bat http://localhost:8081/SSH2/ws/UserWebS?wsdl
用这个命令就可以生成客户端的类供接口调用

方法二:也可以用myeclipse中WSDL Client生成客户端代码。

====================================
客户端访问server端:
Util.java

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Util {

    public static Object getService(Class clazz, String url){
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        Object srv = null;
        factory.setServiceClass(clazz);
        factory.setAddress(url);
        srv = factory.create();
        return srv;
    }
}

UtilWebService.java

import test.webservice.IUserWebService;

public class UtilWebService {

    public static IUserWebService getUserWebService(){
        String url = "http://localhost:8081/SSH2/ws/UserWebS?wsdl";
        IUserWebService service = (IUserWebService) Util.getService(IUserWebService.class, url);
        return service;
    }
}

UserClientTest.java

import test.webservice.IUserWebService;
import test.webservice.User;
import webUtil.UtilWebService;

public class UserClientTest {

    private IUserWebService userWebS;

    public void getUser(){
        User user = new User();
        user.setPassword("ldx");
        user.setUsername("ldx");
        User u = userWebS.getUser(user);
        System.out.println(u.getUid());
    }

    public void getUserByName(String username){
        User u = userWebS.getUserByUsername(username);
        System.out.println(u.getPassword());
    }

    public void addUser(){
        User u = new User();
        u.setPassword("lrh");
        u.setUsername("lrh");
        userWebS.addUser(u);
    }

    public static void main(String[] args) {
        UserClientTest test = new UserClientTest();
        test.userWebS = UtilWebService.getUserWebService();
        test.getUser();
        test.getUserByName("lrh");
        test.addUser();
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21671次
    • 积分:397
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:2篇
    • 译文:0篇
    • 评论:3条
    文章分类