I've encountered this URL RESTful query:
http://rest.ensembl.org/map/cdna/ENST00000288602/100..300?content-type=application/json
Where 100..300 are parameters that says: "from 100 to 300" (indexes). I can change those parameters as i'd like.
I want to have this kind of query parameter passing in my web-service as well. How can I annotate this in a Jersey API, and how do I get the parameter values?
You can use #Path annotation to map the 100..300 kind of URL.
For example, the below code works fine for me.
#Path("/Test")
#Component
public class TestRestfulService {
#GET
#Path("/100..200")
#Produces(MediaType.APPLICATION_JSON)
public String getText(){
return "Success";
}
}
Using the above code you can access the method by localhost:8080/test/100..200.
Hope this solves the problem!
Related
I have JAX-RS 2.8.9 with Spring 4.3.4 app. I perform a very simple POST request to the following server code
#POST
#Consumes({MediaType.APPLICATION_FORM_URLENCODED})
#Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response test(MultivaluedMap<String, String> work) {
return Response.ok(work.keySet().size()).build();
}
I test with curl:
curl -i -X POST 'http://localhost:XXX/some/test' -d "param=value¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded"
I get the following warning
A servlet request to the URI http://localhost:XXX/some/test contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using #FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
About which I found only cause that involve connection issues, apparently I don't have.
According to documentation this is the way to handle a case when we have a variable number of FormParams passed.
This works, though.
#POST
#Consumes({MediaType.APPLICATION_FORM_URLENCODED})
#Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response test(#FormParam("param") String param) {
return Response.ok(param).build();
}
What can be the reason the multivalued map doesn't? Could it be some filtering? What is an alternative for unknown number of parameters?
UPDATE
Is is due to a particularity of Jersey + Spring.
A solution can be found in this answer.
What can be the reason the multivalued map doesn't? Could it be some filtering? What is an alternative for unknown number of parameters?
By default, it seems your JAX-RS implementation is detecting the form-input and reading/processing the body before it gets to your method. Have you tried:
#POST
#Consumes({MediaType.APPLICATION_FORM_URLENCODED})
#Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response test(Form workForm) {
MultivaluedMap<String,String> work = workForm.asMap();
return Response.ok(work.keySet().size()).build();
}
?
Form is a special JAX-RS class that encapsulates all the form parameters and should be usable as an input parameter to your method.
in my Jersey REST interface, there are two methods (among others):
#GET
#Path("{scenarioId}/instance")
#Produces(MediaType.APPLICATION_JSON)
public Response getScenarioInstances(
#Context UriInfo uri,
#PathParam("scenarioId") int scenarioId,
#QueryParam("filter") String filterString) {
// code here
return Response.ok(result.toString(), MediaType.APPLICATION_JSON).build();
}
and
#GET
#Path("{scenarioId}/instance/{instanceId}")
#Produces(MediaType.APPLICATION_JSON)
public Response getScenarioInstance(
#Context UriInfo uri,
#PathParam("scenarioId") int scenarioId,
#PathParam("instanceId") int instanceId) {
// code here
return Response.ok(result.toString(), MediaType.APPLICATION_JSON).build();
}
A GET request via Postman to, say, /2/instance, calls the first method and results in a JSON object containing all instances.
A GET request to e.g. /2/instance/2 however does not call the second method, and results in a 404 error.
Am I missing something in the second method?
Okay, the problem was, that the REST interface was distributed over multiple classes, and there was another class that had a #Path(".../{scenarioId}/instance/{instanceId}) annotation.
I moved the method there, and it was called. Apparently this causes a conflict, even thought there was no #GET method declared for this path directly in this class.
In retrospect, I probably should have added that detail to the question.
I took a look at the Dropwizard framework and I want to use it to package my existing REST service.
In the tutorial I noticed that the response content type is not set using a ResponseBuilder, can I set the reponse type like I would do for a regular REST service if it were not in a Dropwizard framework ?
The reason I want to set a dynamic response content type is because the webservice does not know the kind of data it is serving.
Thanks
You should be able to just return a Response object and adjust the type. For instance:
#Path("/hello")
#Produces(MediaType.TEXT_PLAIN)
public class Example{
#GET
public Response sayHello() {
return Response.ok("Hello world").type(MediaType.TEXT_HTML).build();
}
}
I have a controller that inherits from the org.springframework.web.servlet.mvc.AbstractController class
I have configurated it in this way:
<bean name="/gameServiceController.json" class="xx.xxx.GameController"/>
so can accepts url of this form
http://<hostname>:<port>/<context-path>/gameServiceController.json
but the customer has provided to me the requirement to write URL in this way
http://<hostname>:<port>/<context-path>/createNewGame?parameter=<value>
but I think that is not possible to map this type of URL with my controller. Anyone know the type of configuration that can be used in order to configure this type of URL mapping ?
Otherwise, is legal to ask to change the format of the URL in this way
http://<hostname>:<port>/<context-path>/gameServiceController.json?command=createNewGame&phoneNumber=<phoneNumber>
so I can manage the command parameter in the "handleRequestInternal" method of my custom controller that inherits from the org.springframework.web.servlet.mvc.AbstractController class ??
Don't use the legacy Controller framework, use annotated controllers. There, you can easily use URL templates, something like this:
#Controller
public class GameController{
#RequestMapping(value="/createNewGame?parameter={param}",
method=RequestMethod.GET)
public String createNewGame(#PathVariable String param, Model model) {
// do stuff here
return "viewName";
}
}
I am beginner in REST web services.
I wrote a program of REST to display the HTML or XML. The #Path annotation's value is #Path("{typeDocument}"). There are two methods for GET :
#GET
#Produces(MediaType.TEXT_XML)
public String getXml(#PathParam("typeDocument") String typeDocument)
to display XML file,
and
#GET
#Produces(MediaType.TEXT_HTML)
public String getHtml(#PathParam("typeDocument") String typeDocument)
to display HTML.
The browser Firefox always excutes getHtml() when URL is either
http://localhost:8080/sources/html or http://localhost:8080/sources/xml
But IE always excutes getXml().
How to excute the correct method, as defined by URL, in different browser ?
try using MediaType.APPLICATION_XML instead of TEXT_XML.
That being said, this isn't the best use of JAX-RS - especially if you're using RestEASY or any other implementation with JAXB support.
#GET
#Produces(MediaType.APPLICATION_XML)
#Path("/{typeDocument}")
public MyObject getXml(#PathParam("typeDocument") String typeDocument) {
myObjectService.get(typeDocument);
}
#XmlRootElement(name="myObject")
public class MyObject {
// Some properties
}
would be a much easier method to maintain. You can also use JSPs for the HTML.
See http://java.dzone.com/articles/resteasy-spring for a good example (using Spring).