トップ 一覧 ping 検索 ヘルプ RSS ログイン

JAX-RS Tipsの変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!JAX-RS Tips

[Jersey 2.7 User Guid|https://jersey.java.net/documentation/latest/index.html]
!!!入力
!!Form から POST されるパラメータを取得
 @POST
 @Consumes("application/x-www-form-urlencoded")
 public void post(@FormParam("name") String name) {
 
 }

!!パラメータ
*http://typea.info/tips/wiki.cgi?page=JAX-RS+Tips

,パラメータ,内容
,@QueryParam,URLのクエリパラメータ ?a=b
,@MatrixParam,マトリックスパラメータ セミコロン(;)で区切って入れられる
,@FormParam,HTMLのフォーム
,@CookieParam,クッキーの値を取得
,@HeadeParam,HTTPリクエストヘッダの値を取得

!生のHTTPヘッダーを取得するには
*@Contextを使う
 @GET
 @Produces("text/html")
 public String hoge(@Context HttpHeaders headers) {
     MultivaluedMap<String, String> _header = headers.getRequestHeaders();
              :
 }

!ServletConfig、ServletContext、HttpServletRequest、HttpServletResponse などを取得するには
*@Context を使う
**[ServletConfig|http://docs.oracle.com/javaee/5/api/javax/servlet/ServletConfig.html]
**[ServletContext|http://docs.oracle.com/javaee/5/api/javax/servlet/ServletContext.html]
**[HttpServletRequest|http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html]
**HttpServletResponse
**[Application|https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/Application.html
**[Application|https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/Application.html]
**[UriInfo|https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/UriInfo.html]
**[Request|https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/Request.html]
**[HttpHeaders|https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/HttpHeaders.html]
**[SecurityContext|https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/SecurityContext.html]
**[HttpServletResponse|http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletResponse.html]
**[Providers|https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/ext/Providers.html]

 @GET
 @Produces(MediaType.TEXT_HTML)
 public String greet(
     @Context HttpServletRequest request,
     @Context UriInfo uriInfo) {
 }
!パスパラメータの入力を制限する
*パスに正規表現で制限を行うことが可能
*http://codezine.jp/article/detail/7190?p=2
 @Path("user/{userid: [a-zA-Z][a-zA-Z0-9]*}/{mailAddress}/{phone}") …(1)
 public class UserResource {
     …(省略)…
 }
!!!出力

!!ファイルダウンロード
*[JAX-RSでファイルダウンロードさせる方法|http://dev.worksap.co.jp/Members/t_tanaka/2011/05/31/jax-rs-file-download/]

::上記サイトより
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 @Path("/File")
 public class FileDownloadService {
     @GET
     @Path("/download")
     @Produces(MediaType.APPLICATION_OCTET_STREAM)
     public Response download() {
         StringBuilder sb = new StringBuilder();
         sb.append("sample content");
         InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
         return Response.ok(is)
         .header("Content-disposition", "attachment; filename=data.txt")
         .build();
     }
 }

!添付ファイルのダウンロード
 public Response file(File file, String filename) {
     
     javax.ws.rs.core.Response.ResponseBuilder response 
         = Response.ok((Object) file);
     
     if (StringUtils.isBlank(filename)) {
         filename = "download_file";
     }
     try {
         filename = new String(filename.getBytes("ms932"),"iso-8859-1");
     } catch (UnsupportedEncodingException e) {
         int extPos = filename.lastIndexOf('.');
         if (extPos > 0) {
             filename += filename.substring(extPos);
         }
     }
     
     response.header("Content-Disposition", 
         String.format("attachment; filename=\"%s\"", filename));
     
     return response.build();
 }
!CSVファイルのダウンロード
 public Response csv(String csvContent, String filename) {
 
     ByteArrayInputStream in = new ByteArrayInputStream(csvContent.getBytes());
     
     javax.ws.rs.core.Response.ResponseBuilder response =
         Response.ok(in);
 
     if (StringUtils.isBlank(filename)) {
         filename = "csv_file";
     }
     try {
         filename = new String(filename.getBytes("ms932"),"iso-8859-1");
     } catch (UnsupportedEncodingException e) {
         int extPos = filename.lastIndexOf('.');
         if (extPos > 0) {
             filename += filename.substring(extPos);
         }
     }
     
     response.header("Content-Type", "application/vnd.ms-excel")
             .header("Content-Disposition", 
                     String.format("attachment; filename=\"%s\"", filename));
     
     return response.build();
 }

!共有ディレクトリ上のファイルのダウンロード
*Java Windowsファイル共有サービス(CIFS)経由でファイルを書き込む