[关闭]
@EdwinTang 2016-08-13T23:54:02.000000Z 字数 5893 阅读 1048

soapui之创建Restful/Webservice工程

Restful WebService


Spring+Cxf+maven完成简单的webservice及Restful环境搭建

需要引用的maven包

1.Spring框架需要引入的包

<!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

2.CXF框架需要引入的包

<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-common</artifactId>
        <version>2.5.4</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-core</artifactId>
        <version>2.6.1</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>2.6.1</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

3.Restful协议特别要引入的包

<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.1.6</version>
    </dependency>

需要修改的配置文件

1.Web.xml

<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>  

<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>  

2.applicationContext.xml

<!--此外需要使用ws和rs的命名空间 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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://cxf.apache.org/jaxws  
http://cxf.apache.org/schemas/jaxws.xsd 
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml" />

<!-- WebService配置-->
<bean id="WebServiceSample" class="com.edwin.ws.WebService.WebServiceSampleImpl"/>
<jaxws:endpoint id="addServicePort" implementor="#WebServiceSample"
    address="/addServicePort">
</jaxws:endpoint>

<!--Restful配置-->
<context:component-scan base-package="com.edwin.ws.RestfulService" />
<jaxrs:server address="/rest/add">
    <jaxrs:serviceBeans>
        <ref bean="restfulServiceSampleImpl" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
    </jaxrs:providers>
</jaxrs:server>

接口与实现

一、WebService服务

1.WebService接口

  1. package com.edwin.ws.WebService;
  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebService;
  5. @WebService
  6. public interface WebServiceSample {
  7. @WebMethod
  8. public int addService(@WebParam(name="num1") int num1,@WebParam(name="num2") int num2);
  9. }

2.WebService实现

  1. package com.edwin.ws.WebService;
  2. import javax.jws.WebService;
  3. @WebService(serviceName = "addService",
  4. portName = "addServicePort",endpointInterface = "com.edwin.ws.WebService.WebServiceSample")
  5. public class WebServiceSampleImpl implements WebServiceSample {
  6. public int addService(int num1, int num2) {
  7. return num1+num2;
  8. }
  9. }

二、Restful服务

1.Restful接口

  1. package com.edwin.ws.RestfulService;
  2. import javax.ws.rs.FormParam;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.POST;
  5. import javax.ws.rs.Path;
  6. import javax.ws.rs.PathParam;
  7. import javax.ws.rs.Produces;
  8. import javax.ws.rs.QueryParam;
  9. import javax.ws.rs.core.MediaType;
  10. public interface RestfulServiceSample {
  11. @GET
  12. @Path("/addRestfulServiceQuery")
  13. @Produces(MediaType.APPLICATION_JSON)
  14. int addRestfulService(@QueryParam("num1") int num1,@QueryParam("num2") int num2);
  15. @GET
  16. @Path("/addRestfulServicePath/{num1}/{num2}")
  17. @Produces(MediaType.APPLICATION_JSON)
  18. int addRestfulServicePath(@PathParam("num1") int num1,@PathParam("num2") int num2);
  19. @POST
  20. @Path("/addRestfulServicePost")
  21. @Produces(MediaType.APPLICATION_JSON)
  22. int addRestfulServicePost(@FormParam("num1") int num1,@FormParam("num2") int num2);
  23. }

2.Restful实现

  1. package com.edwin.ws.RestfulService;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class RestfulServiceSampleImpl implements RestfulServiceSample{
  5. public int addRestfulService(int num1, int num2) {
  6. return num1+num2;
  7. }
  8. public int addRestfulServicePath(int num1, int num2) {
  9. return num1+num2;
  10. }
  11. public int addRestfulServicePost(int num1, int num2) {
  12. return num1+num2;
  13. }
  14. }

代码下载地址:https://git.oschina.net/giftboys/WebServiceSample

Restful及Webservice SoapUI下的测试方法

查看CXF服务

1.通过网页查看CXF服务列表。目的两个,一是通过访问以确认服务是否存活,二是通过访问取得WSDL和WADL地址。
cxf服务列表.jpg-56.9kB

处理SOAP协议下的常用接口

1.传入INT类型的接口服务

Java接口

  1. @WebMethod
  2. public int addService(@WebParam(name="num1") int num1,@WebParam(name="num2") int num2);

Java实现

  1. public int addService(int num1, int num2) {
  2. return num1+num2;
  3. }

SoapUI测试
soap协议int类型参数.jpg-43kB

2.传入DATA类型的接口服务

Java接口

  1. @WebMethod
  2. public Date getChangeDay(@WebParam(name="date") Date date,@WebParam(name="offset") int offset);

Java实现

  1. public Date getChangeDay(Date date,int offset) {
  2. Calendar c = Calendar.getInstance();
  3. c.setTime(date);
  4. c.add(Calendar.DAY_OF_YEAR, 1);
  5. Date newDate=c.getTime();
  6. return newDate;
  7. }

SoapUI测试
soap协议date类型参数.jpg-52.8kB

2.当方法内部变更,或方法名变更时SoapUI处理方法

2.1.方法内部发生变更,这里所指的方法内部变更是指计算逻辑,而非参数,方法名等
操作方法:
2.1.1右击服务,选择update Definition
soap协议更新服务方法内容.jpg-210.8kB
2.1.2默认选项,点击确认及可更新
soap协议更新服务方法内容-确认.jpg-65.2kB
2.1.3如果使用者是变更了方法名或参数名而进行了update Definition默认更新,会产生如下的xml导致不能执行成功。从下图中可以看出"2016-05-10T00:00:00.000+08:00"是多余内容。
soap协议更新服务方法内容-参数变更.jpg-45kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注