webService简单记录

一、服务端

接口

package com.test.webservice.service;

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

@WebService
@SOAPBinding(style=Style.RPC)
public interface ServiceMessage {
    
    /**
     * 测试
     * @param userId
     * @return
     */
    public String getTest(String userId);
    
    
}
    

实现

package com.test.webservice.service;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.cxf.jaxws.context.WebServiceContextImpl;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.springframework.beans.factory.annotation.Autowired;


import com.test.service.sys.KeyService;

@WebService(endpointInterface="com.test.webservice.service.ServiceMessage",serviceName="ServiceMessageImpl")
@SOAPBinding(style=Style.RPC)
public class ServiceMessageImpl implements ServiceMessage {

    @Autowired
    UserService userService;
    
    
    @Override
    public String getTest(String userId) {
        User user = userService.getUserById(userId);
        return "欢迎:"+user.getName();
    }

}

这里的@Autowired注解如果单独使用会报错,需要配置spring-mvc.xml,配置如下:

<bean id="serviceMessage" class="com.test.webservice.service.ServiceMessageImpl"></bean>
<jaxws:endpoint id="TestService" implementor="#serviceMessage" address="/ServiceMessage" />

在xml里增加这两行,如果<jaxws:endpoin/>这个标签报错,需要在xml的头部增加xmlns:jaxws="http://cxf.apache.org/jaxws"。

在web.xml配置中增加一下配置:

<!-- CXF的Servlet 测试-->
  <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 本系统的WebService路径必须以/开头 -->
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>

注意,如果是webservice的服务端和项目是在一起的,访问路径不能相同。

二、客户端

建立一个新的项目,做完客户端进行测试。

1.在cmd命令行里输入

wsimport -s F:\\project\\TheClient\\src -p com.test.client -keep http://192.168.0.222:8080/ws/ServiceMessage?wsdl

命令,F:\\project\\TheClient\\src为新项目的src地址,com.test.client为包,http://192.168.0.222:8080/ws/ServiceMessage?wsdl是webservice的访问地址,

2.新建测试类,

package com.test.test;

import com.test.client.ServiceMessage;
import com.test.client.ServiceMessageImpl;

public class TestService {

    public static void main(String[] args) {
        ServiceMessage message = new ServiceMessageImpl().getServiceMessageImplPort();
        String rst = message.getTest"1");
        System.out.println(rst);
    }
}

 

posted @ 2017-08-02 12:40  无知死循环  阅读(424)  评论(0编辑  收藏  举报