Get contextual information such as HttpServletRequest in Jersey

Implementing RESTFul API in Jersey + Spring Framework, I needed the following information.

I checked the method, so I wrote it down.

Environment etc.

Tools etc. Version etc.
MacbookPro macOS Mojave 10.14.5
IntelliJ IDEA Ultimate 2019.3.3
Java AdoptOpenJDK 11
apache maven 3.6.3
Jersey 2.30.1
JUnit 5.6.0
Tomcat apache-tomcat-8.5.51
Postman 7.19.1
Spring Framework 5.2.4-RELEASE

Acquisition method

Just declare it in a member variable with @Context.

@Service
@Path("/my")
class MyResourceApi{
    @Context
    HttpServletRequest httpServletRequest;

    @Context
    HttpServletResponse httpServletResponse;
}

It seems that all other context-related information can be obtained in the same way.

A little more detail

This seems to be due to the annotation-based DI function HK2 that Jersey has built in from version 2 rather than the function of Jersey itself.

For more details, here.

HK2 https://javaee.github.io/hk2/introduction.html

Try the DI function (HK2) built into Jersey2 https://qiita.com/opengl-8080/items/9bdc98aa5269512bd70e

Recommended Posts

Get contextual information such as HttpServletRequest in Jersey
Get EXIF information in Java
Library "OSHI" to get system information in Java
Handle pointers such as UnsafePointer <T> in Swift
[Swift] Get document Id as well as document in Cloud Firestore
How to get information about associated tables in many-to-many tables
[Swift] Get key as well as value in Realtime database
Get location information in Rails and sort in ascending order