In RESTful web services written using Jersey, I know I can access path parameters and query string parameters using #PathParam and #QueryParam annotaions. But in a web service written by someone else I saw a method like below.
#POST
#Path("/sms/receive")
#Consumes("application/json")
#Produces("application/json")
public Response smsReceive(String jsonBody) {
//Code here...
}
There is no #PathParam or #QueryParamannotation before the argument jsonBody.
Can anybody explaing what this argument means and how to set value for it when calling this service.
Can I use multiple parameters without annotations?
Thanks.
The service above does not handle query or path parameters at all.
It #Consumes JSON input. That's what the method's parameter jsonBody is referring to.
If someone would want to instruct this service he would add a json payload to the http request which the service (in this case) receives as a simple String. The String then needs to be parsed.
Of course you can combine Path/Query Parameters with JSON Payloads.
Related
First of all, give thanks for reading my question and try to help me and apologize for my English.
I'm new with Spring and I have this message:
A servlet request to the URI
http://localhost:8080/backend/v1/streetviewer/search-street?url=backend2?busqueda=name%20street&idioma=es-es%26cantidad=10
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.
My backend send a request to backend2 with one parameter (url), but that url contains 3 parameters. I understand that is reason why say that.
But I was reading that #FormParam is used for POST requests and I'm using #QueryParam.
#GET
#Path(ApiPath.PATH_BACKEND2)
public String getDataFromProdServer(#QueryParam(ApiParam.PARAM_URL) final String externalUrl ) {
return mapService.ServerRequest(externalUrl);
}
How can solve it??
To be said you are actually using JAX-RS Implementation, from backend2 I asume it is a separate service so I suggest you to use Spring Implementations for consuming the API. #RequestMapping/#GetMapping and so..
Coming to the question (With Spring Implementation)
#GetMapping(ApiPath.PATH_BACKEND2)
public String getDataFromProdServer(#RequestParam(ApiParam.PARAM_UR) final String externalUrl) {
return mapService.serverRequest(externalUrl);
}
RPC in Internet transport layer, use dto is reasonable. Http controller? If all controller are used by front end, parameter defined as VO?
I guess you are asking whether the argument of the rest controller method can be a DTO.
Well it will depend on the framework you use. The http parameters are strings.
If the framework has an utility mechanism (probably an annotation) that lets you map the http params you receive into a DTO you supply as the rest controller method arg, there's no problem in the arg being a DTO.
If the framework doesn't have such utility (it just maps each http param into an string arg of the rest controller method), then you have to build manually the DTO in the rest controller method.
I don't know if Spring has such an utility annotation similar to #PathVariable but for gathering multiple request params into a DTO object.
UPDATE:
Spring #RequestBody annotation deserializes the JSON into the java object argument of the rest controller method. So, the arg annotated with #RequestBody is a DTO.
DDD says nothing about which type must be the params of a rest api. They can be either a DTO or Strings, it doesn't matter. If they where strings, you would have to construct the DTO by yourself. Using #RequestBody, Spring framework does it for you.
In java, an object that is carries between process is named following the camel case notation and having the DTO suffix.
e.g. ServiceMessageDTO
DTO stands for data transfer object.
This applies also to the request body parameters from the rest webmethods.
I'm defining a RESTful WebService in Java.
It takes as input:
an username (for instance, jdoe);
an URL (for instance: https://blablablabla.io/sample?boh=mah).
By using GET method of the HTTP protocol, it should produce a JSON file.
Which is the best way to pass these params?
In this particular case, is there a best practice to follow in order to properly pass an URL as param?
You must first encode it, you can use
URLEncoder.encode("url");
If you want to define an http GET method, then the only way to pass parameters to it is through the URI query string (ie. ?x=y&...).
This is because GET calls can not take in a message body.
If you want to pass in more complicated information, you will need to use POST, PUT, or some other method. Though, if you are actually just getting information (semantically), then you shouldn't use anything but GET.
Also you can use Path parameters
This is example
#Path("/users")
public class UserResorce {
#GET
#Path("/{username}")
#Produces(MediaType.APPLICATION_JSON)
public String getUser(#PathParam("username") String username)){
}
}
The url is http://domain_name/your_application_path/users/username
I am currently working on a project which was maintained by some other team and now i need to maintain it. While i was going through the project i found some thing below:
In jax-rs controller it was annotated by #Consumes(MediaType.APPLICATION_JSON) but the method takes request body as String rather than JSON. Then what is the use of the annotation? Does it help in content negotiation anyway?
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response createCake(final String requestBody){.......}
How it is converting a JSON body to string?
My technology stack if it anyway helps to answer:
JAX-RS
Spring 3.2
Jersey 2.4
The #Consumes serves the following purpose. It restricts the mapping for your handlers. For example, you may have two handlers for the path /resource, one mapped to consume XML and the other mapped to consume json. The dispatcher will choose the right one based on the request's content-type.
The parameter type can be anything as long as there is an appropriate converter for the specified media type to the parameter type itself. In this case, there's very likely a converter from any media type to String.
Currently I have a web service running in a tomcat (http://localhost:8080/myApp/getUsers). My web service will accept a json string and then process accordingly. My webservice code is as follows:
#Path("/getUsers")
public class UsersWS
{
#POST
public Response post(String theRequestJSON)
{
try
{
JSONObject aJsonObj = new JSONObject(theRequestJSON);
String userID = aJsonObj.getString("userID");
System.out.println(userID);
}
}
}
So, my Web service is processing a json string. So now, I need to call the above web service from another JAVA class (with a jsonObject having the userID in request parameter).
How to do it? Shortly, I need to make a web service call from a JAVA class with a JSON object as a request parameter. How to send a json as a request parameter in a request call.
Take a look at Jersey: http://jersey.java.net
Here's a good write up on how to use the client:
http://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with
Use native URLConnection or Apache HttpClient to send a HTTP request to the server.And the parameters must passed in key=value&key2=value2... format. So you may need to reconstruct the JSON object in that format or using another special parameter name like data=jsonstring then parse the json string using some library.
#George has basically already answered your question, but in terms of JSON processing you may want to also look at Jackson http://jackson.codehaus.org/
This allows you to quickly convert Java objects to JSON equivalents.