Difference between #Path and #WebServlet - rest

In Java EE I notice that you can specify a path to a uri either as
#Path("/SomePath")
public class MyClass
or
#WebServlet("/SomePath")
public class MyClass extends HttpServlet
I think #Path is used for non-servlet stuff while #WebServlet is used for servlets. But do they effectively serve the same purpose?
Info on #Path can be found here:
http://docs.oracle.com/cd/E19798-01/821-1841/6nmq2cp26/index.html
But at first glance, it seems to provide some of the basic functionality as #WebServlet.

#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.

The #Path annotation identifies the URI path template to which the resource responds and is specified at the class or method level of a resource. The #Path annotation’s value is a partial URI path template relative to the base URI of the server on which the resource is deployed, the context root of the application, and the URL pattern to which the JAX-RS runtime responds.
The #WebServlet annotation is used to declare a servlet. The annotated class must extend the javax.servlet.http.HttpServlet class.

Related

Multiple selectors using Sling Servlets

Short version: How do I force the most matching on a servlet based on multiple selectors using Felix annotations for sling servlets?
I have a few servlets defined in an OSGI bundle. I'm using the org.apache.felix.scr.annotations.sling.SlingServlet annotations.
#SlingServlet(
methods={GET},
selectors {"v1"}
...
)
public class MyServlet extends SlingAllMethodsServlet {}
...
#SlingServlet(
methods={GET},
selectors {"v1","special"}
...
)
public class MySpecialServlet extends MyServlet {}
My problem is that I can not find a way to force MySpecialServlet to only handle requests when both selectors are present.
GET http://localhost/my/resource.v1.special.json
Sometimes it will handle requests for only the v1 selector.
GET http://localhost/my/resource.v1.json
It seems that after using posting a new jar through the felix webconsole, if I request the double selector resource.v1.special.json before any other resource, then MySpecialServlet will also continue to handle v1 only requests.
Is there a way I can force the more general servlet to handle the more general list of selectors using the current annotations? Am I missing some part annotation? I believe that this system might be using an older annotation. Perhaps it is worth migrating? I'm trying not to be too intrusive for this small task that I've been asked to do.
Bear with me if I've conflated parts of these technologies. I've just walked up to this problem and I'm still sorting it out. Please correct any misalignment of terms.
Register your MySpecialServlet by v1.special, like selectors = {"v1.special"}.
According to the documentation:
... The selectors must be configured as they would be specified in the URL that is as a list of dot-separated strings such as print.a4 ...
I understand that when registering the servlet by a list of selectors, Sling treats them individually (as within OR condition). So, in the case of registering your special servlet by selectors = {"v1","special"}, the doGet method will be called if you request:
http://localhost/my/resource.v1.special.json or
http://localhost/my/resource.special.json or
http://localhost/my/resource.v1.json

JavaEE REST (Wink) - Can be REST Api class defines as singleton?

I am using J2EE with WASLiberty & Apache Wink (REST Impl).
I would need in some cases to have api class to be singleton, however private constructor probably wont work with REST. Has anyone used api to be singleton ? any hints ?
As per Wink doc
The #Scope annotation
According to the JAX-RS specification, by default, provider and resource classes are instantiated once for each JAX-RS application. This instantiation involves the default constructor for the class being called, with the injection of dependencies happening afterwards.
Since the instantiation of your resource class (in your words, API class) will be carried out by injection, I don't think the default ctor visibility will be an issue (In the worst case, make it public)

spring rest data plural url

I have a domain class called StoreType.java which is exposed by below spring repository
public interface StoreTypeRepository extends PagingAndSortingRepository<StoreType, Short> {
}
When I access this using url http://localhost:8080/my-persistence/jpa/storetypes it returns 404.
if I change my domain class as Storetype (without camel case), it works fine and return 200 OK.
I have few more repositories which uses single world domain classes like Store.java , Country.java and these work fine and by default these exposed as plural of domain class name.
I know spring exposed url as plural of domain classes but not sure why it is not exposing it. I can override this using #RepositoryRestResource(path="/storetypes") but I want to know what is default rest url if domain classes name in camel case.
You seem to have answered the question to your problem by specifying the #RepositoryRestResource( path="/storetypes" ) annotation as the documentation states.
Spring Data REST exposes a collection resource named after the uncapitalized, pluralized version of the domain class the exported repository is handling. Both the name of the resource and the path can be customized using the #RepositoryRestResource on the repository interface.
In this case your naming convention seems correct using StoreTypeRepository however one thing confuses me about your repository definition... I'm not sure why you set the type parameter to the PagingAndSortingRepository<StoreType, Short> but I'm quite certain that's incorrect as the second type parameter should be of type Long.

JAX-RS #PathParam beginning with variable argument

I am using JAX-RS 1.x to develop a simple RESTFul service like /{app_id}/job/list/
So I have defined a resource class like below:
#Path("/{app_id}/job")
#Produces(MediaType.APPLICATION_JSON)
public class JobService {
#GET
#Path("list")
public Response list(#PathParam("app_id") final String appId) {
// ....
}
}
But it seems the implementation (RestEasy in my case) is not able to find this resource. If I make it /job/{app_id}/list, it works but not /{app_id}/job/list.
Is it because I have used variable {app_id} as a beginning path element? Does JAX-RS expects first path element to be fixed?
The root resource is defined as just /.
Is it because I have used variable {app_id} as a beginning path element? Does JAX-RS expects first path element to be fixed?
No, a template parameter can be anywhere in the path and it can be on the resource (= class) or subresource (= method).
Your code works if there is no other JAX-RS resource. Check your system for other #Paths that might match your URI path.

cannot find my bean using the InitialContext.lookup() method

I have tried to use struts 1.3 API to make a small application with EJB 3.0. Unfortunatelly i cannot use the #EJB annotation to call my bean object from inside my action class. I have solved this problem using different workarounds ( the first one is to use my global jndi name of my bean and the other is to call another class first and use the #EJB annotation from that class). Still these two workarounds have significant disadvantages. I would like to call my EJB directly from my action class. I have read plenty examples using the "java:comp/env/beanName" JNDI name but still haven't figure out how to do it and get name not found axception.
Let the full name of the local EJB class be the com.ejb.myEjbPackage.MyEJBLocal, how can i call it using the context lookup? (can i do it without modifying any of the web.xml and sun-web.xml descriptors?)
I am using glassfish server and Netbeans IDE.
Thank you in advance
#EJB won't work in a standard pojo it can only be done in a managed object (i.e. another session bean)
So...
Here's your bean
#Stateless(mappedName="beanName")
public class beanName implements beanNameRemote {
Here's your lookup
Context context = new InitialContext(); //default lookup pulls from jndi properties file
context.lookup("beanName");
You can do some further reading on the mappedName to see if you want to use it or not.
I found the answer :
If you cannot use the EJB annotation in the class you want to call the bean then :
If you don't want to mess with XML descriptors to define your bean , you have to do it in the bean class itself.
Hence i used the following annotation in the GameBean class
#Stateless
#EJB(name="ejb/GameBean",beanInterface=GameBeanLocal.class,beanName="GameBean")
public class GameBean implements GameBeanLocal {.....
The beanName is optional. The annotation must be declared in the line ABOVE the declaration of the class.
Then, in order to call the bean from the other class you can do
InitialContext ic = new InitialContext();
ic.lookup("java:comp/env/ejb/GameBean");