JAX-RS入门 六: 数据处理(1)

接下来要花两小节来介绍一下JAX-RS中的数据处理(Data Handlers)部分。

一、SteamingOutput

在第一节中(http://zzc1684.iteye.com/blog/1674367),看getCustomer()方法:

Java代码  
  1. public StreamingOutput getCustomer(int id) {      
  2.     final Customer customer = customerDB.get(id);      
  3.     if (customer == null) {      
  4.         throw new WebApplicationException(Response.Status.NOT_FOUND);      
  5.     }      
  6.     return new StreamingOutput() {      
  7.         public void write(OutputStream outputStream) throws IOException,      
  8.                 WebApplicationException {      
  9.             outputCustomer(outputStream, customer);      
  10.         }      
  11.     };      
  12. }    

其中使用了SteamingOutput来写一个原始流的字符流。

这是JAX-RS提供的数据处理的其中一种方式,通过回调SteamingOutput的write()方法来写回response。

相对于直接返回一个OutputSteam对象,使用回调对象有以下好处:

  1. JAX-RS可以自由的,按照它的设想处理输出
  2. 当追求性能时,甚至可以使用另一个线程而不是当前调用线程回写
  3. 可以很方便的注入拦截器,而使用一个直接的OutputSteam则通常会跳过这些步骤
  4. 最后,可以实现异步响应,类AJAX。在Servlet3.0中已经介绍了异常响应的想法。

二、InputSteam/Reader

可以使用InputSteam或Reader去处理请求内容,JAX-RS会自动将请求数据转成一个InputSteam/Reader对象,例如:

Inputsteam代码  
  1. @PUT   
  2. @Path("/stuff")   
  3. public void putStuff(InputStream is) {   
  4.     byte[] bytes = readFromStream(is);   
  5.     String input = new String(bytes);   
  6.     System.out.println(input);   
  7. }   
Reader代码  
  1. @PUT   
  2. @Path("/morestuff")   
  3. public void putMore(Reader reader) {   
  4.     LineNumberReader lineReader = new LineNumberReader(reader);   
  5.     do {   
  6.         String line = lineReader.readLine();   
  7.         if (line != null) System.out.println(line);   
  8.     } while (line != null);   
  9. }   

除了处理请求,InputSteam/Reader也可以作为响应:

Response代码  
  1. @GET   
  2. @Path("file/{fileName}")   
  3. @Produces("text/plain")   
  4. public Reader getFileContent(@PathParam("fileName") String fileName);  

注:当作为响应时,需要指定@Produces,这样JAX-RS才知道怎么去设置响应的Content-Type头信息

三、File

File对象也可以用在处理请求或响应中。例如用于请求:

File作为请求参数代码  
  1. @POST   
  2. @Path("/morestuff")   
  3. public void post(File file) {   
  4.     Reader reader = new Reader(new FileInputStream(file));   
  5.     LineNumberReader lineReader = new LineNumberReader(reader);   
  6.     do {   
  7.         String line = lineReader.readLine();   
  8.         if (line != null) System.out.println(line);   
  9.     } while (line != null);   
  10. }  

这里File作为请求参数使用。

注:当使用File作为请求参数时,JAX-RS会在后台生成一个临时文件,以请求的信息体作为这个文件的内容,然后将这个临时文件作为参数传入。

用于响应:

Java代码  
  1. private static final String basePath = "...";   
  2. @GET  
  3. @Path("{filepath: .*}")   
  4. @Produces("text/plain")   
  5. public File getFile(@PathParam("filepath") String path) {   
  6.     return new File(basePath + path);   
  7. }      

注:同样的,当File用作响应时,需要指定@Produces,用于告诉JAX-RS怎么转换File内容,即Content-Type。

四、byte[]

byte[]也可以用在请求或响应,例如:

Java代码  
  1. @GET  
  2. @Produces("text/plain")   
  3. public byte[] get() {   
  4.     return "hello world".getBytes();   
  5. }   
  6. @POST  
  7. @Consumes("text/plain")   
  8. public void post(byte[] bytes) {   
  9.     System.out.println(new String(bytes));   
  10. }  

注:当用作响应时,需要指定@Produces,用于告诉JAX-RS怎么设置响应的Content-Type值。 

五、String/Char[]

大多数网络数据是基于文件格式的。JAX-RS可以进行任何文件格式的内容与String/Char[]之间的转换。例如:

String/char[]代码  
  1. @GET   
  2. @Produces("application/xml")   
  3. public String get() {   
  4.     return "<customer><name>Bill Burke</name></customer>";   
  5. }   
  6. @POST   
  7. @Consumes("text/plain")   
  8. public void post(String str) {   
  9.     System.out.println(str);   
  10. }  

注:当用作响应时,需要指定@Produces,用于告诉JAX-RS怎么设置响应的Content-Type值。

 

注:JAX-RS规范要求实现者必须处理在Content-Type中指定的charset值,当注入String时,例如:

Java代码  
  1. POST /data   
  2. Content-Type: application/xml;charset=UTF-8  
  3. <customer>...</customer>  

这里charset为UTF-8,实现者必须保证生成的Java String必须是UTF-8编码的。

六、MultivaluedMap<String, String> 和Form

在节4(http://liugang594.iteye.com/blog/1496651)中,已经介绍了使用@FormParam去获取提交的Form值。除了使用@FormParam,也可以直接注入MultivaluedMap<String,String>对象来表示所有请求的Form数据,其中Form数据格式是 "application/x-www-form-urlencoded",例如:

Java代码  
  1. @POST  
  2. @Consumes("application/x-www-form-urlencoded")   
  3. @Produces("application/x-www-form-urlencoded")   
  4. public MultivaluedMap<String,String> post(   
  5.     MultivaluedMap<String, String> form) {   
  6.     return form;   
  7. }  

注:JAX-RS规范并未指明注入的MultivaluedMap是否已经编码;大多数实现者都会自动解码其中的key/value值。如果你想保持编码的格式,则可以使用@javax.ws.rs.Encoded注释。

七、javax.xml.transform.Source

Source接口代表了一个XML的输入或输出,它通常是用来进行XSLT转换的,例如:

Java代码  
  1. @Consumes("application/xml")   
  2. @Produces("application/xml")   
  3. public String post(Source source) {   
  4.     javax.xml.transform.TransformerFactory tFactory =   
  5.         javax.xml.transform.TransformerFactory.newInstance();   
  6.     javax.xml.transform.Transformer transformer =   
  7.         tFactory.newTransformer(   
  8.             new javax.xml.transform.stream.StreamSource("foo.xsl"));   
  9.     StringWriter writer = new StringWriter();   
  10.     transformer.transform(source,   
  11.         new javax.xml.transform.stream.StreamResult(writer));   
  12.     return writer.toString();   
  13. }  

除了JAXB(下节讲)外,javax.xml.transform.Source对象是规范中唯一支持的基于XML结构的对象。(甚至不能注入org.w3c.dom.Document对象)

猜你喜欢

转载自zzc1684.iteye.com/blog/1674377