I am trying to write a java client for restful resource. The response for my request is a list of objects. I have the following code for the request. BUt i get some unmarshall exception. Could anyone let me know how to solve this ?
GenericType<List<Response>> genType = new GenericType<List<Response>>() {};
GenericType<List<Response>> response = (GenericType<List<Response>>)resource.path(paramPath).accept(MediaType.APPLICATION_JSON).get(genType);
my resource has the following code
#GET
#Path("/app/{Id}")
#Produces(MediaType.APPLICATION_JSON)
public List<Response> getAllKeyValuesByAppId(#PathParam("Id") Long Id){
...
...
}
Can you test you REST method without unmarshaling?
Are you sure that the message body contains exactly what you expect?
Related
I want to make a GET request to my server that receives two parameters, uniqueConfig and commitHash. The code for this operation in my Controller class is as follows:
#GetMapping("/statsUnique")
public ResponseEntity<Object> hasEntry(#RequestParam("uniqueConfig") String uniqueConfig,
#RequestParam("commitHash") String commitHash) {
Optional<Stats> statsOptional =
codecService.findByUniqueConfigAndCommitHash(uniqueConfig, commitHash);
if (statsOptional.isPresent()) {
return ResponseEntity.status(HttpStatus.OK).body(true);
}
return ResponseEntity.status(HttpStatus.OK).body(false);
}
The issue is, when I try to make the GET request using Postman, the server returns a 400 - Bad Request with the following error message:
MissingServletRequestParameterException: Required request parameter 'uniqueConfig' for method parameter type String is not present]
my JSON on Postman looks like this:
{
"commitHash": "ec44ee022959410f9596175b9424d9fe1ece9bc8",
"uniqueConfig": "bowing_22qp_30fr_29.97fps_fast-preset"
}
Please note that those aren't the only attributes and I've tried making the same request with all of them on the JSON. Nonetheless, I receive the same error.
What am I doing wrong here?
A GET request doesn't (or at least shouldn't) have a body. Parameters defined by the #RequestParam annotations should be sent in the query string, not a JSON body, i.e., the request should be something like
http://myhost/statsUnique?commitHash=commitHash&uniqueConfig=bowing_22qp_30fr_29.97fps_fast-preset
I am writing an blocking web client. I want to return both body(class) and http status code in ResponseEntity object to be used in some method. Can you please help with this. I am new to Java and already tried approaches many mentioned on internet.
You can use ResponseEntity class!
For e.g. Assume you want to return your Model/Pojo as below
Model class -> MyResponse (Has fields, getter/setters,toString etc) you want to respond along with HTTP code
Code snippet
#GetMapping("/getresponse) //Or any mapping
public ResponseEntity getResponse()
{
MyResponse response=new MyResponse();
return new ResponseEntity>(response,HttpStatus.OK);
}
I am using javax to create a REST service to send an Java Object from one system to another.
I send the data like follows:
WebTarget wt = client.target(baseUrl.toString()).path(restUrlSuffix);
response = wt.request(MediaType.APPLICATION_JSON).post(Entity.json(transferJSON));
I defined a method which should receive the entity as a JSON:
#POST
#Path("/post")
#Consumes("application/json")
#Produces("application/json")
public Response saveWorkflowDefinition(#Valid String json) {
.....
.....
String message = "Message to return";
Response res = Response.ok(message).build();
return res;
}
With this method everything is fine. Data arrives as JSON, colud be transformed back to my java class and I can work with the object again.
Also it seems, that the Response is correct.
If I debug my code, the response is properly filled.
But on the side where I want to receive this response and check it, the entity part is empty.
I have no idea why?
Screen 1 is my response before sending it:
Screen 2 is the response after receiving it:
I found a solution.
I had to add a "valid" readEntity to my WebTarget request.
I my case I have written a response object, maybe a String.class might work too. I need my response class later in my code to transfer some more detailed information.
response = wt.request(MediaType.APPLICATION_JSON).post(Entity.json(transferJSON)).readEntity(WFResponse.class);
My requirement is simple. I just can't figure out how to do this. I just started my adventure in learning Rest web services with java. My requirement here is to find the header part of the request to the following web service method.
#GET
#Produces(MediaType.TEXT_HTML)
#Path("/html")
public String getUserHtml(#Context HttpHeaders h){
System.out.println(h.toString());
String responce = "<h1>Hi m8!</h1>";
return responce;
}
As you can see I have tried out something, but this outputs org.glassfish.jersey.server.ContainerRequest#c290c6b
That is not what I want. Can some one tell me how to output the whole header string. I also tried out getHeaderString method but don't know what the argument should be. Thanks.
You can get request header details by calling getRequestHeaders() method of HttpHeaders which will return MultivaluedMap<> object -
MultivaluedMap<String, String> reauestHeaders = h.getRequestHeaders();
Iterate over this map to get the header details.
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();
}
}