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.
Related
I need to use the programmatic API to build a Jersey resource method and define its handling method/class/inflector. At the end, the resource should look like something like this:
#Path("helloworld")
public class HelloWorldResource {
#POST
#Consumes("text/plain")
public String getHello(
#CustomEntityAnnotation("World") CustomEntityClass name) {
return "Hello " + name.toString() + "!";
}
}
But I need to build many such resources with different values for paths and more importantly: different values for the #CustomEntityAnnotation annotation.
Let me add that this annotation must later be accessed by a ContainerResponseFilter, and by a MessageBodyWriter<CustomEntityClass>.
Unfortunately, I can't find in the docs where I shall add this annotation to the resource Method model builder, and what should be the signature of the method handler.
Any help would be very much appreciated.
Kind regards,
Maxime
I want to append the query parameters list of a received UriInfo in a Rest service. The query comes from the client with some parameters and I need to add some more in server side.
I tried with:
uriInfo.getQueryParameters().add("Param", "value");
but when I display the URI, it doesn't has the new parameter.
On the other hand, if I do it like this:
URI uri = uriInfo.getRequestUriBuilder().queryParam("Param", "value").build();
when I display the URI, it contains the new parameter. The problem in this second case is to reconstruct a UriInfo object to give to the next functions, they require it.
I've seen that it cannot be instantiated, it has no constructors, it has to be added with #Context, its value can be updated by another UriInfo... but how to create this UriInfo with the URI I modified?
It is not possible to modify a UriInfo, there are no methods defined for that. The only option is to recreate it using one implementation of the interface. The only implementation available is org.jboss.resteasy.spi.ResteasyUriInfo.
The problem is that when deployed, and the function using it is called, it throws a ClassDefNotFound exception; even with a dependency in the manifest pointing to resteasy-jaxrs-2.3.2.Final.jar
So, the only option is to make our own implementation of the interface.
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.
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.
The client may request two urls with the same path but different query strings:
1. /hello
2. /hello?name=Mike
How to use jersey to define two different methods for each of them?
Here is an example:
#Path("/hello")
public class HelloResource {
#Produces("text/plain")
public String justHello() {}
// how to call this method only the query string has "name"
#Produces("text/plain")
public String helloWithName() {}
}
You can't do this, Jersey only matches the path. See http://jersey.java.net/nonav/documentation/latest/jax-rs.html for full details.
You can build your own switch based on the query parameter being present. In general, name=mike is not very RESTy anyways. Jersey does support:
/hello/{name}
And that's the way it's meant to be used.
Actually you can. You just need to have a single method mapped to /hello/ that checks for the query parameter. If it exists, delegate to another method as a sub-resource.
http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e374