I'm using Resteasy with Quarkus (io.quarkus.quarkus-resteasy).
I have a path with params declared on a controller.
#RequestScoped
#Path("/v1/domain/{domain}/resource")
public class MyRestController {
#POST
#Consumes(APPLICATION_JSON)
public Response create(Entity entity) {
// here I create a new entity...
}
#GET
#Path("/{id}")
#Produces(MediaType.APPLICATION_JSON)
public Response get(#PathParam("id") String id) {
// get and return the entity...
}
}
I would like to retrieve the domain path param from outside this controller, in a provider marked with #Dependent for example, or in any interceptor that process the incoming request.
#Dependent
public class DomainProvider {
#Produces
#RequestScoped
public Domain domain() {
// retrieve the path param here !
}
}
I didn't find a way to do that, nor documentation about this.
I tried both:
injecting io.vertx.ext.web.RoutingContext with #Inject and access routingContext.pathParams()
using ResteasyProviderFactor to recover the request context data
In both case, there is no path parameter : the request path is resolved as a simple string, containing the actual URL the client used to contact my web service.
Edit:
As a workaround, in my DomainProvider class, I used the routingContext to retrieve the called URL and a regular expression to parse it and extract the domain.
There is no standard way to do this.
You need to pass the param from the JAX-RS resource on down to whatever piece of code needs it
Related
I am working on Quarkus application and what I want to do is to set the global path from application.properties file for all rest rest, my application is working but while calling rest request it is giving not found 404.
#ApplicationScoped
public class ABC {
#POST
#javax.ws.rs.Path("/callit")
public Uni<Response> deleteNoti()
{
//whatever logic
}
}
#ApplicationScoped
public class PAR {
#POST
#javax.ws.rs.Path("/callitPar")
public Uni<Response> addNoti()
{
//whatever logic
}
}
And in application.properties file I am configuring below properties:
quarkus.resteasy.path=/rest/*
quarkus.rest.path=/rest/*
quarkus.http.root-path=/myapp
but when I am calling rest request from front-end it is not working, my rest request should be as below:
http://localhost:8080/myapp/rest/callit
http://localhost:8080/myapp/rest/callitPar
What I want is every rest request should start with "/rest/*" and my application base URL should be "/myapp", Let me know how can we achieve it?
Try to annotate your resource classes with #Path("/") and set quarkus.resteasy.path=/rest.
This should result in your described behaviour.
quarkus.rest.path can be removed.
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getRscSubTypes(){
return AddResourceMysql.getRscSubType();
}
#GET
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String getDbTypes() {
return AddResourceMysql.getDbType();
}
This is returning the following exception:
org.glassfish.jersey.server.model.ModelValidationException:
Validation of the application resource model has failed during application initialization.
Can you please help me?
How request matching works
Definitely, you can have more than one method annotated with #GET in the same class. However, your current definition is ambiguous.
For more clarification, have a look at the JAX-RS 2.0 specification:
3.7.2 Request Matching
A request is matched to the corresponding resource method or sub-resource method by comparing the normalized request URI, the media type of any request entity, and the requested response entity format to the metadata annotations on the resource classes and their methods. [...]
How to fix it
You need change your method annotations to ensure you have no ambiguity. To do it, you can play with the following annotations:
HTTP method: #GET, #POST, #PUT, #DELETE, #HEAD and #OPTIONS
Request URI: #Path
Media type of any request entity: #Consumes
Requested response entity format: #Produces
To fix it, for example, you can just add a #Path annotation with different values to each method.
If you want to define multiple resource methods, which handle GET requests for the same MIME type, within the same class, you have to specify a different subpath for the methods:
#Path("rcsubtypes")
#GET
#Produces(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public String getRscSubTypes()
{
return AddResourceMysql.getRscSubType();
}
#Path("dbtypes")
#GET
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String getDbTypes()
{
return AddResourceMysql.getDbType();
}
The path, specified in the #Path annotation of this method, is a subpath of the path specified in the #Path annotation of the class, which is a subpath of the path you defined for your application.
To explain your behaviour, that always the second method is called, if there is no #Consumes annotation present on the first method: #Consumes defines which media type (set in the Content-Type header of the request) can be accepted by the method. Without a #Consumes annotation all requests are accepted, but i think, if a method specifies an accepted media-type, it will be preferred.
The matching section in the jersey documentation: 3.1. Root Resource Classes
Lets say this is my JAX-RS resource class
#Path("/books")
class Book {
#GET
#PATH("{id}")
public Book get() {
}
}
So, when i make a request for "{context}/books/123", its gets to my Book.get() method.
Now, I have a Servlet where I want the resource class name (and method) by the URL? Is there a way to query JAX-RS for the class name that handles a specific resource?
I have a Jersey 1.8 application running. Jersey is running as a Servlet.
I need to write a servlet filter that given a plain request/response, is able to figure out which REST resource/method will respond to the request and extract values from annotations.
For example, imagine I have the following resource:
#Path("/foo")
#MyAnnotation("hello")
public class FooResource {
#GET
#Path("/bar")
#MyOtherAnnotation("world")
public Response bar(){
...
}
}
When a request GET /foo/bar comes in, I need my servlet filter to be able to extract the values "hello" and "world" from MyAnnotation and MyOtherAnnotation before Jersey's own servlet processes the request.
This filter logic should be able to work for all requests and all resources registered.
Is there a way to access Jersey's internal routing mechanism to obtain a class/method reference where Jersey will dispatch the request?
I'm open to other suggestions as well, but ideally nothing like trying to hack my own routing mechanism by reading the #Path annotations myself.
#Provider
#Priority(Priorities.AUTHORIZATION)
public class MyFilter implements ContainerRequestFilter
#Context // request scoped proxy
private ResourceInfo resourceInfo;
#Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (resourceInfo.getResourceClass().isAnnotationPresent(MyAnnotationion.class) ||
resourceInfo.getResourceMethod().isAnnotationPresent(MyOtherAnnotation.class)) {
to register the filter use
bind(AuthFilter.class).to(ContainerRequestFilter.class).in(Singleton.class);
I am writing a web service like
#Path("/pathName")
public class LoginServiceComponent {
#GET
#Path("/methodPathName/{param}")
#Produces(MediaType.TEXT_HTML)
public String getVoterByVoterId( #PathParam("param") String param)
{
.................
}
}
Here my url to access web service is http://www.abc.com/pathName/methodPathName/1
Here i have 10 methods.Is there any possibility to remove class level #Path means i have only one web service class in my project.So i dont want to use class level #Param repeatedly.
Thanks in advance...
If you want to avoid the #Path on the class so your URL's don't have the "pathName" in the path, I don't think you can remove the #Path on the class entirely. But I have used the #Path class annotation of #Path("/") and was able to get just URL to be just http://www.abc.com/methodPathName/1 (if that's what you're trying to do).