Spring中应用Apache Wink创建REST服务

本文详细介绍了如何在Spring框架中整合ApacheWink组件来构建RESTful Web服务,包括Maven项目的构建方式、关键配置文件设置、以及通过自定义类实现资源注册和暴露。同时提供了使用Java客户端进行调用的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文简单介绍怎么在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数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值