| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「JAX-RS Tips」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
 
1行目: 1行目:
==JAX-RS Tips==
+
==[[JAX-RS Tips]]==
  
[[https://jersey.java.net/documentation/latest/index.html Jersey 2.7 User Guid]] |
+
[https://jersey.java.net/documentation/latest/index.html Jersey 2.7 User Guid]
 
==入力==
 
==入力==
 
===Form から POST されるパラメータを取得===
 
===Form から POST されるパラメータを取得===
11行目: 11行目:
  
 
===パラメータ===
 
===パラメータ===
*http://typea.info/tips/wiki.cgi?page=JAX-RS+Tips
+
*http://typea.info/tips/wiki.cgi?page=[[JAX-RS]]+Tips
  
 
{|class="wikitable"
 
{|class="wikitable"
18行目: 18行目:
 
|-
 
|-
 
|@QueryParam
 
|@QueryParam
|URLのクエリパラメータ ?a=b
+
|U[[R]]Lのクエリパラメータ ?a=b
 
|-
 
|-
 
|@MatrixParam
 
|@MatrixParam
24行目: 24行目:
 
|-
 
|-
 
|@FormParam
 
|@FormParam
|HTMLのフォーム
+
|[[HTML]]のフォーム
 
|-
 
|-
|@CookieParam
+
|@[[Cookie]]Param
 
|クッキーの値を取得
 
|クッキーの値を取得
 
|-
 
|-
 
|@HeadeParam
 
|@HeadeParam
|HTTPリクエストヘッダの値を取得
+
|[[HTTP]]リクエストヘッダの値を取得
 
|-
 
|-
 
|}
 
|}
  
====生のHTTPヘッダーを取得するには====
+
====生の[[HTTP]]ヘッダーを取得するには====
 
*@Contextを使う
 
*@Contextを使う
 
  @GET
 
  @GET
 
  @Produces("text/html")
 
  @Produces("text/html")
  public String hoge(@Context HttpHeaders headers) {
+
  public String hoge(@Context Http[[Header]]s headers) {
     MultivaluedMap<String, String> _header = headers.getRequestHeaders();
+
     MultivaluedMap<String, String> _header = headers.getRequest[[Header]]s();
 
               :
 
               :
 
  }
 
  }
  
====ServletConfig、ServletContext、HttpServletRequest、HttpServletResponse などを取得するには====
+
====ServletConfig、ServletContext、HttpServlet[[R]]equest、HttpServlet[[R]]esponse などを取得するには====
 
*@Context を使う
 
*@Context を使う
 
**[http://docs.oracle.com/javaee/5/api/javax/servlet/ServletConfig.html ServletConfig]
 
**[http://docs.oracle.com/javaee/5/api/javax/servlet/ServletConfig.html ServletConfig]
 
**[http://docs.oracle.com/javaee/5/api/javax/servlet/ServletContext.html ServletContext]
 
**[http://docs.oracle.com/javaee/5/api/javax/servlet/ServletContext.html ServletContext]
 
**[http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html HttpServletRequest]
 
**[http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html HttpServletRequest]
**HttpServletResponse
+
**HttpServlet[[R]]esponse
 
**[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 Application]
 
**[https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/UriInfo.html UriInfo]
 
**[https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/core/UriInfo.html UriInfo]
58行目: 58行目:
  
 
  @GET
 
  @GET
  @Produces(MediaType.TEXT_HTML)
+
  @Produces(MediaType.TEXT_[[HTML]])
 
  public String greet(
 
  public String greet(
     @Context HttpServletRequest request,
+
     @Context HttpServlet[[R]]equest request,
 
     @Context UriInfo uriInfo) {
 
     @Context UriInfo uriInfo) {
 
  }
 
  }
 
====パスパラメータの入力を制限する====
 
====パスパラメータの入力を制限する====
*パスに正規表現で制限を行うことが可能
+
*パスに[[正規表現]]で制限を行うことが可能
 
*http://codezine.jp/article/detail/7190?p=2
 
*http://codezine.jp/article/detail/7190?p=2
 
  @Path("user/{userid: [a-zA-Z][a-zA-Z0-9]*}/{mailAddress}/{phone}") …(1)
 
  @Path("user/{userid: [a-zA-Z][a-zA-Z0-9]*}/{mailAddress}/{phone}") …(1)
  public class UserResource {
+
  public class User[[R]]esource {
 
     …(省略)…
 
     …(省略)…
 
  }
 
  }
82行目: 82行目:
 
  import javax.ws.rs.Produces;
 
  import javax.ws.rs.Produces;
 
  import javax.ws.rs.core.MediaType;
 
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
+
  import javax.ws.rs.core.[[R]]esponse;
 
  @Path("/File")
 
  @Path("/File")
  public class FileDownloadService {
+
  public class FileDownloadSer[[vi]]ce {
 
     @GET
 
     @GET
 
     @Path("/download")
 
     @Path("/download")
     @Produces(MediaType.APPLICATION_OCTET_STREAM)
+
     @Produces(MediaType.APPLICATION_OCTET_ST[[R]]EAM)
     public Response download() {
+
     public [[R]]esponse download() {
 
         StringBuilder sb = new StringBuilder();
 
         StringBuilder sb = new StringBuilder();
 
         sb.append("sample content");
 
         sb.append("sample content");
 
         InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
 
         InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
         return Response.ok(is)
+
         return [[R]]esponse.ok(is)
 
         .header("Content-disposition", "attachment; filename=data.txt")
 
         .header("Content-disposition", "attachment; filename=data.txt")
 
         .build();
 
         .build();
99行目: 99行目:
  
 
====添付ファイルのダウンロード====
 
====添付ファイルのダウンロード====
  public Response file(File file, String filename) {
+
  public [[R]]esponse file(File file, String filename) {
 
      
 
      
     javax.ws.rs.core.Response.ResponseBuilder response  
+
     javax.ws.rs.core.[[R]]esponse.[[R]]esponseBuilder response  
         = Response.ok((Object) file);
+
         = [[R]]esponse.ok((Object) file);
 
      
 
      
 
     if (StringUtils.isBlank(filename)) {
 
     if (StringUtils.isBlank(filename)) {
122行目: 122行目:
 
  }
 
  }
 
====CSVファイルのダウンロード====
 
====CSVファイルのダウンロード====
  public Response csv(String csvContent, String filename) {
+
  public [[R]]esponse csv(String csvContent, String filename) {
 
   
 
   
 
     ByteArrayInputStream in = new ByteArrayInputStream(csvContent.getBytes());
 
     ByteArrayInputStream in = new ByteArrayInputStream(csvContent.getBytes());
 
      
 
      
     javax.ws.rs.core.Response.ResponseBuilder response =
+
     javax.ws.rs.core.[[R]]esponse.[[R]]esponseBuilder response =
         Response.ok(in);
+
         [[R]]esponse.ok(in);
 
   
 
   
 
     if (StringUtils.isBlank(filename)) {
 
     if (StringUtils.isBlank(filename)) {

2020年2月16日 (日) 04:28時点における最新版

JAX-RS Tips

Jersey 2.7 User Guid

入力

Form から POST されるパラメータを取得

@POST
@Consumes("application/x-www-form-urlencoded")
public void post(@FormParam("name") String name) {

}

パラメータ

パラメータ 内容
@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 などを取得するには

@GET
@Produces(MediaType.TEXT_HTML)
public String greet(
    @Context HttpServletRequest request,
    @Context UriInfo uriInfo) {
}

パスパラメータの入力を制限する

@Path("user/{userid: [a-zA-Z][a-zA-Z0-9]*}/{mailAddress}/{phone}") …(1)
public class UserResource {
    …(省略)…
}

出力

ファイルダウンロード

上記サイトより
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)経由でファイルを書き込む