Let's say I have a Jersey JAX-RS api end-point for handling http://<some_path>/foo. Ignore the ....
#Path("foo")
public class FooResource
#GET
#Produces("application/json")
public response getMethod(...)
I want to create POST end-point for foo/{id}/bar, where id is a path parameter and there's a body associated with the HTTP POST.
Example: HTTP POST foo/1/bar with body: { data : "...." }.
How can I add this POST method within the FooResource class? I tried an inner class, but it didn't work when I tested with Postman.
#POST
#Path("{id}/bar")
#Produces("application/json")
public response myPostMethod(...)
You can have path at method level. This will have your post method accessible via /foo/{id}/bar
Related
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
i am using simple java and jersey for rest. below is method. from postman i am sending json request and want to retrieve this data into method without declaring POJO class. but unable to retrieve.
#Path("{entity}/markLabel")
#PUT
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public Response markLabel()
throws Exception {
}
i want to retrieve request json parameter(fileType,groupId,sourceId) into method this request json parameter.
this is header part
can someone help me in this?
Why would you want to retrieve the request parameters or headers in another method?
One method handles one request, thats the way, but if you want to isolate some part of the functionality into seperate method you can call the other method inside the request handling method.
#Path("{entity}/markLabel")
#PUT
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public Response markLabel(ReqObject String reqBody) throws Exception {
//////do something
someOtherMethod(reqBody);
//////do something
}
class ReqObject{
String fileType;
String groupId;
String sourceId;
//getters and setters
}
We created Jersey service for client with two classes, we used First class with Content type Multiple Part Form Data -- #POST #Consumes(MediaType.MULTIPART_FORM_DATA) and in Second class we used only #POST as per our requirement. Service is working fine but client is hitting to Application without any content type so request is mapping with first class which lead to error.
As per my query it should match with 2nd class #POST if we do not use any content Type.
In 1st Class
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response submitForm(
In 2nd class
#POST
public Response submitForm(
QUESTION : POST requests without Content-Type header pass throught #Consumes check.
I'm at the beginning of creating a Webservice with Java.
I want to POST a XML-Request to a Restful Webservice and the Response should be a modified XML. So actually just the Root-Element of the Request should be changed and it should be added another element.
Request:
<Request>
<name>name</name>
</Request>
Response:
<Response>
<name>name</name>
<status>created</status>
</Response>
Currently I'm only returning the Request.
Which is the best/easiest way to modify the Request? Can I do it with JAXB?
public class Resource {
#POST
#Produces
#Consumes
public Request request(Request r) {
return r;
}
}
It seems you are using Jersey to expose the rest api. As RedFive mentioned there are plenty of examples available to build rest api using jersey(jax-rs implementation) on internet. I did a small POC while learning jersey. You can find a sample POST API implementation here. I am passing request bean(as Person class object). The request json/xml is unmarshalled to Person object. Instead of returning object of Request type, you have to return Response object. Hope this example helps you in learning jersey.
#POST
#Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
#Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response makeRequest(Request req) {
Response res = new Response();
res.setName(req.getName());
return Response.entity(res).status(Response.Status.CREATED).build();
}
One thing I want to point out that you may not return the status in the response body. You return 201(CREATED) http status code which resembles the same thing.
I have two webs services or endpoints having one method each and each method is mapped with one URL. I am calling one webservice URL from REST client and in this method i want to call method in another web service which is mapped to URL. How can this be achieved in apache CXf ?
I tried using HttpClient to call another webservice from one but I am getting 404, if I use complete URL and getting 302 code but no response if I use relative URL. what might be issue and what is correct approach ?
You can try to call directly the other controller without httprequest.
for example you have the two next controllers with all annotations you need
#RestController
#RequestMapping("/a")
public class A{
#RequestMapping(...)
public void toCall(){
//your code
}
}
you want to call the method toCall of controller A from controller B
#RestController
#RequestMapping("/b")
public class B{
#RequestMapping(...)
public void method(){
A a = new A();
a.toCall();
}
}