Apache Wink是一个用于帮助开发者快速高效的开发RESTful Web services服务的java框架,他包括一个服务器端模块和一个客户端模块.
1.创建基于wink的web服务.
重点:通过如下两行代码可以返回给客户端一个流文件.
ResponseBuilder responseBuilder = Response.ok(fis, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Response response = responseBuilder.header("test", "test").build();
4.在java客户端,其中contentType("application/xml;charset=UTF-8").accept("application/xml;charset=UTF-8")分别代表客户端发送的请求
媒体类型和可接受请求的媒体类型.
重点:通过如下两行代码可以获取服务器返回流文件的头信息和流文件.
Map<String, List<String>> header = response.getHeaders();
InputStream is = response.getEntity(new EntityType<InputStream>(){});
1.创建基于wink的web服务.
a.用maven构建rest服务器端项目,直接看代码pom.xml.
<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ilucky.rest</groupId>
<artifactId>rest-server</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-server</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.3</version>
</dependency>
</dependencies>
</project></span>
b.将cmd窗口切换到rest-server目录下执行mvn eclipse:myeclipse-clean eclipse:myeclipse命令下载pom.xml文件中配置的类库.c.在web.xml中将所有rest服务映射到RestServerApplication中,rest通过Application管理所有服务,直接看代码RestServerApplication.
<span style="font-size:12px;">package com.iluck.rest.server;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
/**
* @author IluckySi
* @date 20140802
*/
public class RestServerApplication extends Application {
private Set<Object> svc_singletons = new HashSet<Object>();
private Set<Class<?>> svc_classes = new HashSet<Class<?>>();
public RestServerApplication() {
svc_singletons.add(RestServerServiceImpl.getInstance());
}
@Override
public Set<Object> getSingletons() {
return svc_singletons;
}
@Override
public Set<Class<?>> getClasses() {
return svc_classes;
}
}
</span>
d.创建rest服务,直接看代码RestServerService.
<span style="font-size:12px;">package com.iluck.rest.server;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
/**
* @author IluckySi
* @date 20140802
*/
@Path("/download")
public interface RestServerService {
@POST
@Produces("*;charset=UTF-8")
@Consumes("*;charset=UTF-8")
public Response download(String userInfo);
}
</span>
<span style="font-size:12px;">package com.iluck.rest.server;
import java.io.File;
import java.io.FileInputStream;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
/**
* @author IluckySi
* @date 20140802
*/
public class RestServerServiceImpl implements RestServerService {
private static RestServerServiceImpl instance = null;;
public synchronized static RestServerServiceImpl getInstance() {
if(instance == null) {
instance = new RestServerServiceImpl();
}
return instance;
}
public Response download(String message) {
System.out.println("rest服务接收指令:" + message);
Response response = null;
FileInputStream fis = null;
try {
File file = new File("E:/sdx.zip");
fis = new FileInputStream(file);
ResponseBuilder responseBuilder = Response.ok(fis, MediaType.APPLICATION_OCTET_STREAM_TYPE);
response = responseBuilder.header("test", "test").build();
} catch (Exception e) {
System.out.println("下载文件失败!");
}
return response;
}
}
/**
输出结果:
rest服务接收指令:download
*/
</span>
2.创建基于wink的java客户端.a.用maven构建rest客户端项目,直接看代码pom.xml.
<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ilucky.rest</groupId>
<artifactId>rest-client</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-client</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.3</version>
</dependency>
</dependencies>
</project></span>
b.将cmd窗口切换到rest-client目录下执行mvn eclipse:myeclipse-clean eclipse:myeclipse命令下载pom.xml文件中配置的类库.c.创建rest客户端,直接看代码RestClientService
.
<span style="font-size:12px;">package com.ilucky.rest.client;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.EntityType;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
/**
* @author IluckySi
* @date 20140802
*/
public class RestClientService {
public static void main(String[] args) {
//创建rest客户端.
RestClient restClient = new RestClient();
Resource resource = restClient.resource("http://127.0.0.1:8080/rest-server/rest/download");
final ClientResponse response = resource.post("download");
//接收服务器返回媒体的头信息.
Map<String, List<String>> header = response.getHeaders();
System.out.println("服务器返回状态: " + response.getStatusCode());
System.out.println("服务器返回文件头信息-测试: " + header.get("test"));
System.out.println("服务器返回文件头信息-返回大小: " + header.get("Content-Length"));
System.out.println("服务器返回文件头信息: " + header);
//接收服务器返回媒体流.
InputStream is = response.getEntity(new EntityType<InputStream>(){});
//将流写入指定文件.
save(is);
}
public static void save(InputStream is) {
try {
File file = new File("E:/temp/ilucky.zip");
if(!file.exists()) {
file.createNewFile();
} else {
return;
}
int buffer = 10240;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);
byte[] buf = new byte[buffer];
int length = 0;
while ((length = bis.read(buf, 0, buffer)) != -1) {
bos.write(buf, 0, length);
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bos != null) {
bos.close();
bos = null;
}
if(fos != null) {
fos.close();
fos = null;
}
if(bis != null) {
bis.close();
bis = null;
}
if(is != null) {
is.close();
is = null;
}
} catch (Exception e) {
System.out.println("关闭流发生问题!");
}
}
} catch (Exception e) {
System.out.println("下载文件发生问题!");
}
}
}
/**
输出结果:
服务器返回状态: 200
服务器返回文件头信息-测试: [test]
服务器返回文件头信息-返回大小: [672]
服务器返回文件头信息: CaseInsensitiveMultivaluedMap [map=[null=HTTP/1.1 200 OK,Content-Length=672,Content-Type=application/octet-stream,Date=Sat, 02 Aug 2014 07:07:01 GMT,Server=Apache-Coyote/1.1,test=test]]
*/
</span>
3.在web服务端,其中@Produces("*;charset=UTF-8")和@Consumes("*;charset=UTF-8")分别代表服务返回的媒体类型和可接受请求的媒体类型.重点:通过如下两行代码可以返回给客户端一个流文件.
ResponseBuilder responseBuilder = Response.ok(fis, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Response response = responseBuilder.header("test", "test").build();
4.在java客户端,其中contentType("application/xml;charset=UTF-8").accept("application/xml;charset=UTF-8")分别代表客户端发送的请求
媒体类型和可接受请求的媒体类型.
重点:通过如下两行代码可以获取服务器返回流文件的头信息和流文件.
Map<String, List<String>> header = response.getHeaders();
InputStream is = response.getEntity(new EntityType<InputStream>(){});