笔记:Jersey REST 传输格式
Posted 追寻自由的路途
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了笔记:Jersey REST 传输格式相关的知识,希望对你有一定的参考价值。
通常REST接口会以XML或JSON作为主要传输格式,同时 Jersey 也支持其他的数据格式,比如基本类型、文件、流等格式。
- 基本类型
Java的基本类型又叫原生类型,包括4种整数(byte、short、int、long)、2种浮点类型(float、double)、Unicode编码的字符(char)和布尔类型(boolean),Jersey 支持全部的基本类型,还支持与之相关的引用类型,示例代码如下:
- REST 服务代码
@POST
@Path("b")
public String postBytes(final byte[] bs) {
for (final byte b : bs) {
System.out.print(Integer.toHexString(b) + " ");
}
? ?
return "byte[]:" + new String(bs);
}
- 单元测试
@Test
public void postBytesTest() {
final String message = "TEST STRING";
final Invocation.Builder request = target.path("demos").path("b").request();
final Response response = request.post(Entity.entity(message, MediaType.TEXT_PLAIN_TYPE), Response.class);
String result = response.readEntity(String.class);
? ?
System.out.println("result:" + result);
? ?
Assert.assertTrue("字符串不匹配", result.equals("byte[]:" + message));
}
- 文件类型
Jersey 支持传输 File 类型的数据,以方便客户端直接传递File类实例给服务端,文件类型的请求,默认使用的媒体类型是 Content-Type:text/html,示例代码如下:
- REST服务代码
@POST
@Path("file")
public String uploadFile(final File file) {
StringBuilder sb = new StringBuilder(1024000 * 5);
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
byte[] buff = new byte[102400];
int readCount;
String dir = System.getProperty("user.dir");
File outputFile = new File(dir + "\\updateFile");
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
do {
readCount = bufferedInputStream.read(buff, 0, buff.length);
if (readCount > 0) {
bufferedOutputStream.write(buff, 0, readCount);
}
} while (readCount > 0);
}
return "ok";
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
- 单元测试
@Test
public void PostFileTest() {
URL resource = getClass().getClassLoader().getResource("TortoiseGit-2.3.0.0-64bit.msi");
File file = new File(resource.getFile());
Invocation.Builder builder = target.path("demos").path("file").request();
Response response = builder.post(Entity.entity(file, MediaType.TEXT_HTML_TYPE));
if (response.getStatus() == 200) {
String result = response.readEntity(String.class);
System.out.println(result);
} else {
System.out.println("response status=" + response.getStatus() + "\tmessage=" + response.getStatusInfo());
}
}
- 字节流和字符流类型
Jersey 支持Java的两大读写模式,即字节流和字符流
- 字节流:
- REST服务代码
@POST
@Path("fileInputStream")
public String uploadFileByInputStream(final InputStream inputStream) {
StringBuilder sb = new StringBuilder(1024000 * 5);
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
byte[] buff = new byte[102400];
int readCount;
String dir = System.getProperty("user.dir");
File outputFile = new File(dir + "\\updateFile");
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
do {
readCount = bufferedInputStream.read(buff, 0, buff.length);
if (readCount > 0) {
bufferedOutputStream.write(buff, 0, readCount);
}
} while (readCount > 0);
}
? ?
return "ok";
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
- 单元测试
@Test
public void PostFileByInputStreamTest() {
InputStream inputStream = null;
try {
inputStream = getClass().getClassLoader().getResource("TortoiseGit-2.3.0.0-64bit.msi").openStream();
? ?
Invocation.Builder builder = target.path("demos").path("fileInputStream").request();
Response response = builder.post(Entity.entity(inputStream, MediaType.TEXT_PLAIN_TYPE));
if (response.getStatus() == 200) {
String result = response.readEntity(String.class);
System.out.println(result);
} else {
System.out.println("response status=" + response.getStatus() + "\tmessage=" + response.getStatusInfo());
}
} catch (IOException e) {
e.printStackTrace();
}
}
- 字符流:
- REST服务代码
@POST
@Path("fileReader")
public String uploadFileByReader(final Reader reader) {
StringBuilder sb = new StringBuilder(1024000 * 5);
try (BufferedReader bufferedReader = new BufferedReader(reader)) {
byte[] buff = new byte[102400];
String readLine;
String dir = System.getProperty("user.dir");
File outputFile = new File(dir + "\\updateFile.xml");
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outputFile))) {
do {
readLine = bufferedReader.readLine();
if (readLine != null) {
bufferedWriter.write(readLine);
bufferedWriter.newLine();
}
} while (readLine != null);
}
return "ok";
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
- 单元测试
@Test
public void PostFileByReaderTest() {
InputStream inputStream = null;
try {
inputStream = getClass().getClassLoader().getResource("POM.xml").openStream();
? ?
Invocation.Builder builder = target.path("demos").path("fileReader").request();
Response response = builder.post(Entity.entity(inputStream, MediaType.TEXT_PLAIN_TYPE));
String result = response.readEntity(String.class);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
? ?
? ?
以上是关于笔记:Jersey REST 传输格式的主要内容,如果未能解决你的问题,请参考以下文章