本文简单介绍怎么在spring中应用Apache Wink创建REST服务。该例子使用maven创建web项目,项目架构如下
maven引用如下
web.xml中配置spring和apache wink的servlet,如下所示。
需要注意的是对 classpath:META-INF/server/wink-core-context.xml的配置,
其位于wink-spring-support中,是wink与spring集成的核心配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:META-INF/server/wink-core-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>restServlet</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml中配置如下,Registrar类主要完成对Provider和Resource的注册工作。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.apache.wink.spring.Registrar">
<property name="classes">
<set value-type="java.lang.Class">
<value>org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider</value>
</set>
</property>
<property name="instances">
<set>
<ref local="myRestServer" />
</set>
</property>
</bean>
<bean id="myRestServer" class="myserver.MyRestServer" />
</beans>
MyRestServer类
package myserver;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/")
public class MyRestServer {
@GET
@Path("/sayHello")
@Produces(MediaType.APPLICATION_JSON)
public String sayHello(@QueryParam("name") String name){
return "Hello," + name;
}
}
将项目部署于tomcat下运行,用浏览器测试如下
也可以用java测试类,如下
package test;
import javax.ws.rs.core.MediaType;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
public class MyClient {
public static void main(String[] args) {
RestClient client = new RestClient();
Resource resource = client.resource("http://localhost:8080/myrest/rest/sayHello?name=China");
String response = resource.accept(MediaType.APPLICATION_JSON).get(String.class);
System.out.println("返回的结果:"+response);
}
}
这个例子是以rest提供JSON数据。