Accessing context-param values from within a RESTEasy provider - java

I have created a MessageBodyReader/MessageBodyWriter that needs to
read a configuration value. Ideally I'd like this to be held in the
web.xml as a context-param. Is there any way that a RESTEasy provider
can access context params? Is there any type that I can inject using
#Context that will allow me to get context-param values? I haven't
been able to find one.
Alternatively, is there a better way to provide configuration values
to a provider? I'd like to avoid having to use a system property.

In answer to my own question, one solution is to use Spring.
When RESTEasy beans are created via the the Spring context, one can easily provide configuration values in the application-context.xml. It's even possible to use context-param values from the web.xml using the ServletContextPropertyPlaceholderConfigurer.

Related

I want to using #WebServlet annotation and #Path annotation to same time in jetty

First, Please forgive me for my clumsy English.
[What I want to do]
I want to know #WebServlet annotation by Servlet 3.0 and #Path annotation by Jersey 2.22.2, It is able to using same time?
[That I want is to help]
Can I use two annotations at the same time?
If I can use those annotations, is that way how to use?
Thank you.
#Path annotation defines a path to a RESTful Web service so when you have #Path("/SomeService") it will translate into www.yourapp.com/baseRestUrl/SomeService. You can also define it on the methods which provides REST services. Note that baseRestUrl is defined inside web.xml or in class which extends Application class.
On the other hand #WebServlet("/SomePath") states that Servlet will be listening for request on the www.yourapp.com/SomePath, it is basically replacement of servlet-mapping element in web.xml. You can still configure servlets like this, it's up to you whether you prefer XML or annotation configuration.

Calling a method with using bean and reflection

I have some classes and i want declare these classes in bean xml file. I want to call methods from these classes with reflection. I mean i have two feature in bean tag, one 'id' and another 'class', I want when take id value to a methods in reflection, find class and call special method. anyway, i searched about java beans, reflection and etc but i cant use these and i want to see a simple tutorial because my knowledge is very basic and little :(
How can i use bean xml file in reflection for calling a method from special class that the class declare in bean file?
for using bean xml file, i should install spring framework? and where should i create xml file with beans tag?(i mean i should create this in special directory, like below src or below res...)
Thanks for advises
For spring web application,By default you can declare beans in
/WEB-INF/applicationContext.xml
You can also declare beans in someName.xml in separate folder inside /WEB-INF/ like
/WEB-INF/config/database.xml
which could be imported in /WEB-INF/applicationContext.xml or other like
<import resource="/WEB-INF/config/database.xml"/>
and register ApplicationContext using ContextLoaderListener in web.xml as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
for more information look spring docummentation.
http://docs.spring.io/spring/docs/1.2.9/reference/beans.html
if you are doing android then there is spring-android library but it doesn't seems to help you.
Why should I use Spring Android?
you might need to write your bean declaration file as plain xml inside resource folder and
res/xml/
see:
http://developer.android.com/guide/topics/resources/providing-resources.html
additionally you will have to implement your logic reading beans from that xml file.
From the Spring page:
Features
A Rest Client for Android
Auth support for accessing secure APIs
Spring android does not have bean configuration. Why do you need it? What you want may be done with generics and reflection. Could you provide an example of why you want that?

Runtime loading of Controllers for Spring MVC and dynamically mapping requests/URLs

We are starting a new project using Spring MVC, and we would like to move away from annotation-driven request/url mapping. We wish to implement the following use case:
Use Case A
User enters a URL.
The request mapping handler retrieves a list of mappings (e.g. from the DB), and based on this dynamic list of mappings, it calls the relevant controller.
This is because we want to be able to do the following as well:
Use Case B
We want to load a new Controller (perhaps a new reports module) into the web app without having to redeploy or do a server restart.
We will map this new Controller to a URL and persist it somewhere (most likely the DB).
We would like the Controller to be registered in the Spring app context (managed by Spring).
We would then like to use this new Controller in the request mapping.
We've taken an initial look at the different ways we can implement this, but we are unsure of the best architecture/method to go about this route. A couple of questions:
For Use Case A, how do we implement this within the Spring MVC framework (or if it's possible)?
For Use Case B, is there a good framework or way to be able to do dynamically loading and registering of this for web applications? We've taken a cursory look at OSGI but it seems to be advisable for use in non-web applications.
For Use case A :
Instead of DB you can keep the url mappings in a property file and then use property place holder to initialize beans using xml configuration on context up. This way remaining inside the spring framework, you can avoid annotations.
For Use Case B :
Tomcat supports dynamic reloading of classes but that to of only non structural changes in class file. But this has memory leaks as well as it doesnt cleans up old instance of class loader rather it creates a new instance.
Its quite achievable using spring-mvc-router API.
Please check below link
url-action mapping & routing in Spring MVC 3.0
Here the URL can be configured to controller.method using .conf file, but this can be achievable using java configuration, and i haven't tried so far.
Also if xml configuration chosen, then check out the property 'autoReloadEnabled', but its not adviceable for production use.
Hope this helps!!!

Session aware spring bean

Is there any way to define a spring bean which will be notified when data in session has changed?
I would also like to know pure java solution if possible. All I want is when i add/edit/delete data in httpsession then I want one java class to be notified to do some processing on that data.
Thanks
You don't need Spring for this, the Servlet API provides that out-of-the-box, via the HttpSessionAttributeListener interface:
This listener interface can be implemented in order to get notifications of changes to the attribute lists of sessions within this web application.
You declare it as a <listener> in your web.xml file. See here for an example.
In don't know of a specific Spring-friendly way of doing this, though, I think you'll have to use the above listener approach, and notify your Spring beans from there.

Configure EJB3 with web.xml like parameters

I'd like to pass some parameters to a stateless session bean. I'd like to do something like I do when I configure a Servlet initial parameters in the web.xml file. Thanks
It is traditional to use a property file for this.

Categories