「JAX-RS Tips」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==JAX-RS Tips== [https://jersey.java.net/documentation/latest/index.html Jersey 2.7 User Guid] ==入力== ===Form から POST されるパラメータを取得===…」) |
|||
| (同じ利用者による、間の2版が非表示) | |||
| 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] | ||
| 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 | ||
| − | | | + | |U[[R]]Lのクエリパラメータ ?a=b |
|- | |- | ||
|@MatrixParam | |@MatrixParam | ||
| 24行目: | 24行目: | ||
|- | |- | ||
|@FormParam | |@FormParam | ||
| − | | | + | |[[HTML]]のフォーム |
|- | |- | ||
| − | |@ | + | |@[[Cookie]]Param |
|クッキーの値を取得 | |クッキーの値を取得 | ||
|- | |- | ||
|@HeadeParam | |@HeadeParam | ||
| − | | | + | |[[HTTP]]リクエストヘッダの値を取得 |
|- | |- | ||
|} | |} | ||
| − | ==== | + | ====生の[[HTTP]]ヘッダーを取得するには==== |
*@Contextを使う | *@Contextを使う | ||
@GET | @GET | ||
@Produces("text/html") | @Produces("text/html") | ||
| − | public String hoge(@Context | + | public String hoge(@Context Http[[Header]]s headers) { |
| − | MultivaluedMap | + | MultivaluedMap<String, String> _header = headers.getRequest[[Header]]s(); |
: | : | ||
} | } | ||
| − | ==== | + | ====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] | ||
| − | ** | + | **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. | + | @Produces(MediaType.TEXT_[[HTML]]) |
public String greet( | public String greet( | ||
| − | @Context | + | @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 | + | 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. | + | import javax.ws.rs.core.[[R]]esponse; |
@Path("/File") | @Path("/File") | ||
| − | public class | + | public class FileDownloadSer[[vi]]ce { |
@GET | @GET | ||
@Path("/download") | @Path("/download") | ||
| − | @Produces(MediaType. | + | @Produces(MediaType.APPLICATION_OCTET_ST[[R]]EAM) |
| − | public | + | 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 | + | return [[R]]esponse.ok(is) |
.header("Content-disposition", "attachment; filename=data.txt") | .header("Content-disposition", "attachment; filename=data.txt") | ||
.build(); | .build(); | ||
| 99行目: | 99行目: | ||
====添付ファイルのダウンロード==== | ====添付ファイルのダウンロード==== | ||
| − | public | + | public [[R]]esponse file(File file, String filename) { |
| − | javax.ws.rs.core. | + | javax.ws.rs.core.[[R]]esponse.[[R]]esponseBuilder response |
| − | = | + | = [[R]]esponse.ok((Object) file); |
if (StringUtils.isBlank(filename)) { | if (StringUtils.isBlank(filename)) { | ||
| 111行目: | 111行目: | ||
} catch (UnsupportedEncodingException e) { | } catch (UnsupportedEncodingException e) { | ||
int extPos = filename.lastIndexOf('.'); | int extPos = filename.lastIndexOf('.'); | ||
| − | if (extPos | + | if (extPos > 0) { |
filename += filename.substring(extPos); | filename += filename.substring(extPos); | ||
} | } | ||
| 122行目: | 122行目: | ||
} | } | ||
====CSVファイルのダウンロード==== | ====CSVファイルのダウンロード==== | ||
| − | public | + | public [[R]]esponse csv(String csvContent, String filename) { |
ByteArrayInputStream in = new ByteArrayInputStream(csvContent.getBytes()); | ByteArrayInputStream in = new ByteArrayInputStream(csvContent.getBytes()); | ||
| − | javax.ws.rs.core. | + | javax.ws.rs.core.[[R]]esponse.[[R]]esponseBuilder response = |
| − | + | [[R]]esponse.ok(in); | |
if (StringUtils.isBlank(filename)) { | if (StringUtils.isBlank(filename)) { | ||
| 136行目: | 136行目: | ||
} catch (UnsupportedEncodingException e) { | } catch (UnsupportedEncodingException e) { | ||
int extPos = filename.lastIndexOf('.'); | int extPos = filename.lastIndexOf('.'); | ||
| − | if (extPos | + | if (extPos > 0) { |
filename += filename.substring(extPos); | filename += filename.substring(extPos); | ||
} | } | ||
2020年2月16日 (日) 04:28時点における最新版
目次
JAX-RS Tips
入力
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 などを取得するには
- @Context を使う
@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 {
…(省略)…
}
出力
ファイルダウンロード
上記サイトより
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)経由でファイルを書き込む
© 2006 矢木浩人