#navi_header|Java| Jerseyの勉強メモ。 JAX-RSレベルで規定されている仕様については [[1283]] も参照。 - Jersey -- https://jersey.java.net/ --- GlassFish プロジェクトでも使われてる、Sunマイクロシステムズ時代からのJAX-RSのリファレンス実装。ライセンスはCDDLv1.1とGPLv2のデュアルライセンス。 練習サンプル: - Grizzly2版 : https://github.com/msakamoto-sf/jaxrs2-exercise-jersey - Tomcat7版 : https://github.com/msakamoto-sf/jaxrs2-exercise-jersey-servlet * Jerseyを使ったREST APIサーバとクライアントを作成するチュートリアル - REST with Java (JAX-RS) using Jersey - Tutorial -- http://www.vogella.com/tutorials/REST/article.html - Jersey と Apache Tomcat を使って RESTful な Web サービスを作成する -- http://www.ibm.com/developerworks/jp/web/library/wa-aj-tomcat/ - java - Get HttpServletRequest in Jax Rs / Appfuse application? - Stack Overflow -- http://stackoverflow.com/questions/9612194/get-httpservletrequest-in-jax-rs-appfuse-application --- HttpServletRequestを直接参照したい場合、"@Context"アノテーションでインジェクトする。 * "Content-Disposition: attachment" でファイルをダウンロードさせたい StreamingObjectクラスをimplementしてResponseのentityにセットする。 参考サイト: - JAX RS: Streaming a Response using StreamingOutput | Javalobby -- http://java.dzone.com/articles/jax-rs-streaming-response - Serving Files using Jersey Web Service (JAX-RS) | Matthias Kerstner -- http://www.kerstner.at/en/2013/08/serving-files-using-jersey-web-service-jax-rs/ - jersey Example of using StreamingOutput as Response entity - Stack Overflow -- http://stackoverflow.com/questions/12012724/jersey-example-of-using-streamingoutput-as-response-entity - java - How to set response header in JAX-RS so that user sees download popup for Excel? - Stack Overflow -- http://stackoverflow.com/questions/4621748/how-to-set-response-header-in-jax-rs-so-that-user-sees-download-popup-for-excel * HttpServletResponseを直接参照・操作する→あんまりオススメできないかも。 JAX-RS 2.0の仕様としてはHttpServletResponseを直接参照、操作することも可能だが、副作用の発生する操作の組み合わせによっては期待通りに動かないケースも。 - Jersey - Serving Binary Content (Files) Through Jersey -- http://jersey.576304.n2.nabble.com/Serving-Binary-Content-Files-Through-Jersey-td2691376.html --- HttpServletResponseを直接使う方法の参考。リソースのメソッドの戻り値をvoidにする方法も紹介されているが、Jerseyだと警告メッセージが出る。 - java - HTTPServletResponse status code is not in HTTP response header - Stack Overflow -- http://stackoverflow.com/questions/11671190/httpservletresponse-status-code-is-not-in-http-response-header --- 「HttpServletResponse.setStatus()したのが反映されない、なんで!?」に対して、HttpServletResponseでデータの出力まではしてないため、JAX-RS側でリソースクラスが本当にServletResponseを触ったのかどうか分からず、リソースクラスのメソッドもStringを通常通り返しているため、JAX-RS側としては200を返すしか無い、とのこと。解決方法として、ステータスコードを指定できるWebApplicationException()をthrowする方法がアドバイスされている。 - [JERSEY] HOW TO FORCE EMPTY RESPONSE ON ERROR -- https://java.net/projects/jersey/lists/users/archive/2012-08/message/125 --- Jerseyで、頑張って直接HttpServletResponseでエラー時に空のレスポンスボディを返そうとしたが上手く行かなかったので、結局Responseクラスをbuildして返すようにしたケース。 #navi_footer|Java|