wink restful服务 第一个例子记录

  REST(Representational State Transfer)  based Web Service是相对于传统的Web Service(SOAP+WSDL+UDDI)而提出的。传统的Web Service可以很好的解决异构系统之间的通信问题,但是需要首先定义好XML格式的合同(WSDL),client和server都必须严格遵守协议,不容易升级以及集群伸缩。REST Web Service不需要事先定义格式,传输的内容也可以依据不同的client变化(json,xml,html等),最重要的是使用源URL来唯一定位资源,对资源的增删改查映射为HTTP的四个方法,无状态传输,具有非常好的伸缩性

     Apache Wink就是一个纯Java的REST框架。它完整的实现了JSR 311并扩展了部分功能,此外还提供了良好的扩展性,难能可贵的是还可以与流行的Java框架Spring无缝集成。目前该项目还在开发中。所谓框架无非就是定义好格式,提供一些工具和钩子,让开发人员可以专注于业务逻辑的开发。本文将试图简单的介绍一下Wink。

1、下载wink官网包

当前   apache-wink-1.3.0

 

 jar包引入:

在一个搭好了spring的环境里,添加:

 1、wink-1.3.0.jar apache-wink-1.3.0\dist\路径下,该包包含了主要的wink逻辑,有了这个无需再引入wink-client-1.3.0.jar、wink-common-1.3.0.jar、wink-server-1.3.0.jar

2、slf4j-api-1.6.1.jar    javax.ws.rs支持,apache-wink-1.3.0\lib下
3、slf4j-simple-1.6.1.jar   javax.ws.rs支持,apache-wink-1.3.0\lib下
4、jsr311-api-1.1.1.jar   apache-wink-1.3.0\lib下


提供对普通pojo转成json的支持需要加入jackson包:
jackson-core-asl-1.9.2.jar   自己找
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
以及wink的jackson转换包
wink-jackson-provider-1.3.0.jar    apache-wink-1.3.0\ext\wink-jackson-provider 下

 

2、配置

     Web Service当然是Web程序了,所以入口就是一个Servlet,在web.xml里面配置一下,把REST的访问都安排给Wink来处理。代码如下:

 

<servlet>
		<servlet-name>restSdkService</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>restSdkService</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>

 

 

 

    与Spring的集成,需要讲wink包里的wink-core-context.xml载入,配置如下:

 

 

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:META-INF/server/wink-core-context.xml,classpath:config/spring/spring-config.xml</param-value>
	</context-param>

 
其中wink-core-context.xml是wink-spring-support模块里面的一个Spring配置文件

 

spring的配置文件app-context.xml添加bean如下:

<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="helloWorldResource" />
			</set>
		</property>
	</bean>

	
	<bean id="helloWorldResource" class="training.HelloWorldResource" />

 

 

 

HelloWorldResource是一个REST里面的Resource,用Annotation配置路径等信息:

 

package training;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.wink.common.annotations.Workspace;
import org.springframework.stereotype.Service;

import training.module.xuelei.data.Xuelei;

@Workspace(workspaceTitle = "Workspace Title", collectionTitle = "Collection Title")
@Path("/testWink")
@Service
public class HelloWorldResource {

	@GET
	public String getMessage() {
		return "HelloWorld";
	}
	
	
	@GET
	@Path("/getjson/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public XueleiNew getById(@PathParam("id") String id)
	{
	    XueleiNew xl = new XueleiNew();
	    xl.setId("123");
	    xl.setName("小李子");
		return xl;
	}
	
	@GET
	@Path("/getxml/{id}")
	@Produces(MediaType.APPLICATION_XML)
	public Book getBook(@PathParam("id") String id){
		Book b = new Book();
		b.setId(123111);
		b.setISBN("sdfjlsjdf2kj234");
		b.setTitle("一个农民工的江湖");
		
		return b;
		

	}
}

 

 

pojo对象:

转成的json的对象无需作任何处理

 

package training;

import java.io.Serializable;


public class XueleiNew implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	private String id;
	
	private String name;
    
	public XueleiNew(){
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

   
}

 

 

转成xml的对象需要添加注释

 

package training;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlRootElement(name="book")
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {

	@XmlAttribute(name="id")
	private int id;

	@XmlElement(name="title")
	private String title;

	@XmlElement(name="isbn")
	private String ISBN;



	
	public Book() { }

	public int getId() { 
		return id; 
	}
	public void setId(int id) { 
		this.id = id; 
	}

	public String getTitle() { 
		return title; 
	}
	public void setTitle(String title) {
		this.title = title; 
	}

	public String getISBN() { 
		return ISBN; 
	}
	public void setISBN(String ISBN) {
		this.ISBN = ISBN; 
	}


	
}

 

 

这样启动web服务器,输入http://localhost:8080/weat/rest/即可看到web service信息,服务列表里面可以看到HelloWorldResource服务。

http://localhost:8080/training.web/rest/testWink/getjson/123123  获得对象转换成的json

{"id":"123","name":"小李子"}

 

http://localhost:8080/training.web/rest/testWink/getxml/1235345  获得对象转换成的xml

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  <book id="123111">
  <title>一个农民工的江湖</title>
  <isbn>sdfjlsjdf2kj234</isbn>
  </book>

 

 

 

部分内容抄袭自:http://blog.csdn.net/kimylrong/article/details/7687593

 

这里还有一个不错的wink例子:http://download.csdn.net/download/kehongyong/4167213

 

猜你喜欢

转载自1924357316.iteye.com/blog/1933404