设为首页 - 加入收藏 PHP编程网 - PHP站长网 (http://www.52php.cn)- 电商,百科,编程,业界,移动互联,5G,云计算,站长网!
热搜: 专业 娱乐 applewat 服务
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-01-24 10:41 所属栏目:[大数据] 来源:网络整理
导读:接下来要花两小节来介绍一下JAX-RS中的数据处理(Data Handlers)部分。 ? 一、SteamingOutput 在第一节中(http://liugang594.iteye.com/blog/1491434),看getCustomer()方法: Java代码?? public?StreamingOutput?getCustomer(int?id)?{????? ????final

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

?

一、SteamingOutput

在第一节中(http://liugang594.iteye.com/blog/1491434),看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代码??

收藏代码

    @PUT??
  1. @Path("/stuff")??
  2. public?void?putStuff(InputStream?is)?{??
  3. ????byte[]?bytes?=?readFromStream(is);??
  4. ????String?input?=?new?String(bytes);??
  5. ????System.out.println(input);??
  6. }??

?

Reader代码?? @Path("/morestuff")??
  • public?void?putMore(Reader?reader)?{??
  • ????LineNumberReader?lineReader?=?new?LineNumberReader(reader);??
  • ????do?{??
  • ????????String?line?=?lineReader.readLine();??
  • ????????if?(line?!=?null)?System.out.println(line);??
  • ????}?while?(line?!=?null);??
  • 除了处理请求,InputSteam/Reader也可以作为响应:

    Response代码?? @GET??
  • @Path("file/{fileName}")??
  • @Produces("text/plain")??
  • public?Reader?getFileContent(@PathParam("fileName")?String?fileName);??
  • 注:当作为响应时,需要指定@Produces,这样JAX-RS才知道怎么去设置响应的Content-Type头信息

    三、File

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

    File作为请求参数代码?? @POST??
  • public?void?post(File?file)?{??
  • ????Reader?reader?=?new?Reader(new?FileInputStream(file));??
  • 这里File作为请求参数使用。

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

    用于响应:

    private?static?final?String?basePath?=?"...";??
  • @GET??
  • @Path("{filepath:?.*}")??
  • @Produces("text/plain")??
  • public?File?getFile(@PathParam("filepath")?String?path)?{??
  • new?File(basePath?+?path);??
  • }??????
  • 注:同样的,当File用作响应时,需要指定@Produces,用于告诉JAX-RS怎么转换File内容,即Content-Type。

    四、byte[]

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

    byte[]?get()?{??
  • return?"hello?world".getBytes();??
  • }??
  • @POST??
  • @Consumes("text/plain")??
  • void?post(byte[]?bytes)?{??
  • ????System.out.println(new?String(bytes));??
  • 注:当用作响应时,需要指定@Produces,用于告诉JAX-RS怎么设置响应的Content-Type值。?

    五、String/Char[]

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

    String/char[]代码?? @Produces("application/xml")??
  • public?String?get()?{??
  • ????return?"<customer><name>Bill?Burke</name></customer>";??
  • @Consumes("text/plain")??
  • public?void?post(String?str)?{??
  • ????System.out.println(str);??
  • 注:当用作响应时,需要指定@Produces,用于告诉JAX-RS怎么设置响应的Content-Type值。

    ?

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

    POST?/data??

  • Content-Type:?application/xml;charset=UTF-8??
  • <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",例如:

    @Consumes("application/x-www-form-urlencoded")??

  • @Produces("application/x-www-form-urlencoded")??
  • public?MultivaluedMap<String,String>?post(??
  • ????MultivaluedMap<String,?String>?form)?{??
  • return?form;??
  • 注:JAX-RS规范并未指明注入的MultivaluedMap是否已经编码;大多数实现者都会自动解码其中的key/value值。如果你想保持编码的格式,则可以使用@javax.ws.rs.Encoded注释。

    七、javax.xml.transform.Source

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

    @Consumes("application/xml")??

  • @Produces("application/xml")??
  • public?String?post(Source?source)?{??
  • ????javax.xml.transform.TransformerFactory?tFactory?=??
  • ????????javax.xml.transform.TransformerFactory.newInstance();??
  • ????javax.xml.transform.Transformer?transformer?=??
  • ????????tFactory.newTransformer(??
  • ????????????new?javax.xml.transform.stream.StreamSource("foo.xsl"));??
  • ????StringWriter?writer?=?new?StringWriter();??
  • ????transformer.transform(source,??
  • new?javax.xml.transform.stream.StreamResult(writer));??
  • return?writer.toString();??
  • 除了JAXB(下节讲)外,javax.xml.transform.Source对象是规范中唯一支持的基于XML结构的对象。(甚至不能注入org.w3c.dom.Document对象)

    【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

    推荐文章
    热点阅读