Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAX-RS resource lifecycle

 
Souvik Dasgupta
Ranch Hand
Posts: 95
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the below code:




web.xml


According to JAX-RS spec: "By default a new resource class instance is created for each request to that resource" and also "Root resource classes are instantiated by the JAX-RS runtime and MUST have a public constructor for which the JAX-RS runtime can provide all parameter values.".

I found is that constructor fired before I actually hit the JAX-RS resource.Message inside constructor got printed when I hit index.jsp. My question is - Is this the way it works? I thought It will get fired when JAX-RS resource is actually hit.

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try to answer your query
JAX-RS resource classes are simple POJOs. Resource has been defined with Servlet settings in web.xml with <load-on-startup> parameter as 1. This load-on-startup element is causing Jersey Servlet to load JAX-RS resource before first request.
If you do not specify a <load-on-startup> element, the servlet container will typically load your servlet when the first request arrives for it.
By setting a <load-on-startup> element, you can tell the servlet container to load the servlet as soon as the servlet container starts.
So try removing this load-on-startup element and then try to run your example.
 
Souvik Dasgupta
Ranch Hand
Posts: 95
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.Understood.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic