Start with Jersey framework
Jersey framework is mainly used in developing restful API,
to develop simple API as web services.
how can we use the jersey framework with java technology?
when we need fast development in rest API as a service, then we need to add dependency in pom.xml
for this we need to create a dynamic project using any Development IDE which supports java language.
Dependency need to add in the pom.xml file
main dependency artifactId jersey-container-servlet-core its a dependency which provide
core features of jersey, and it's part of org.glassfish.jersey.containers.
but it provides in many versions we can use in any
version according to our project need
jersey-server is also a dependency which is also an important part of the jersey
framework and you download from org. glassfish
JSON-conversion we need to add some Jackson dependencies also
important Jackson modules are there like
Jackson-module-parameter-names this dependency provided by
com.fasterxml.jackson.module
Jackson-datatype-jdk8 is also our Jackson module dependency so this
dependency provided by com.fasterxml.jackson.datatype
its mainly used for JSON parameters in data type
Nowadays we can also use jersey framework with spring boot
for this, we need to add the dependency to the spring boot project pom.xml
for spring boot jersey
spring-boot-starter-jersey dependency is provided by org.spring framework.boot
So now let us start with the main structure of the jersey framework
If any request comes from the client end then that request check-in web.xml
in web.xml
need to declare load-on-startup tag which has some parameter with it,
like servlet-mapping tag will start URL-pattern tag which provides API
endpoint
then that request map to particular action/controller servlet ist process
done by jersey.config.server.provider.packages its part of servlet-class
org.glassfish.jersey.servlet.ServletContainer
then there is our param-value for controller or action class location
After getting a proper request
in-class its have a @path annotation map with its where an endpoint of
API declares in it.
I suppose its a get request then we can use inbuilt @GET annotation
with that method, with its need to declare @produce("application/json")
for JSON API if we have an XML request then application/xml need to be
used in that place.
In get method, @Context HttpServletRequest request, need to pass in
it to get request parameter with it.
OrderedJSONObject is a class used to send responses, it basically
extend JSONObject class which provide a method to convert the output in
JSON format, this class are not threaded safe.
So we need to create an object of OrderedJSONObject so we can put
response output values in it, then return write(true); method.
Thank you for sharing this informative and valuable content here.
ReplyDeleteGet the awesome coding snippets at
source code
Visit for more Education repositories
great work
ReplyDeleteGreat
ReplyDelete