@EdwinTang
2016-08-13T23:54:02.000000Z
字数 5893
阅读 1048
Restful
WebService
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>
1.WebService接口
package com.edwin.ws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface WebServiceSample {
@WebMethod
public int addService(@WebParam(name="num1") int num1,@WebParam(name="num2") int num2);
}
2.WebService实现
package com.edwin.ws.WebService;
import javax.jws.WebService;
@WebService(serviceName = "addService",
portName = "addServicePort",endpointInterface = "com.edwin.ws.WebService.WebServiceSample")
public class WebServiceSampleImpl implements WebServiceSample {
public int addService(int num1, int num2) {
return num1+num2;
}
}
1.Restful接口
package com.edwin.ws.RestfulService;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
public interface RestfulServiceSample {
@GET
@Path("/addRestfulServiceQuery")
@Produces(MediaType.APPLICATION_JSON)
int addRestfulService(@QueryParam("num1") int num1,@QueryParam("num2") int num2);
@GET
@Path("/addRestfulServicePath/{num1}/{num2}")
@Produces(MediaType.APPLICATION_JSON)
int addRestfulServicePath(@PathParam("num1") int num1,@PathParam("num2") int num2);
@POST
@Path("/addRestfulServicePost")
@Produces(MediaType.APPLICATION_JSON)
int addRestfulServicePost(@FormParam("num1") int num1,@FormParam("num2") int num2);
}
2.Restful实现
package com.edwin.ws.RestfulService;
import org.springframework.stereotype.Component;
@Component
public class RestfulServiceSampleImpl implements RestfulServiceSample{
public int addRestfulService(int num1, int num2) {
return num1+num2;
}
public int addRestfulServicePath(int num1, int num2) {
return num1+num2;
}
public int addRestfulServicePost(int num1, int num2) {
return num1+num2;
}
}
代码下载地址:https://git.oschina.net/giftboys/WebServiceSample
1.通过网页查看CXF服务列表。目的两个,一是通过访问以确认服务是否存活,二是通过访问取得WSDL和WADL地址。
Java接口
@WebMethod
public int addService(@WebParam(name="num1") int num1,@WebParam(name="num2") int num2);
Java实现
public int addService(int num1, int num2) {
return num1+num2;
}
SoapUI测试
Java接口
@WebMethod
public Date getChangeDay(@WebParam(name="date") Date date,@WebParam(name="offset") int offset);
Java实现
public Date getChangeDay(Date date,int offset) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DAY_OF_YEAR, 1);
Date newDate=c.getTime();
return newDate;
}
SoapUI测试
2.1.方法内部发生变更,这里所指的方法内部变更是指计算逻辑,而非参数,方法名等。
操作方法:
2.1.1右击服务,选择update Definition
2.1.2默认选项,点击确认及可更新
2.1.3如果使用者是变更了方法名或参数名而进行了update Definition默认更新,会产生如下的xml导致不能执行成功。从下图中可以看出"2016-05-10T00:00:00.000+08:00"是多余内容。