Attention! You can not strain on the decision, just tell me in which direction to think!

We have a very simple service:

@GET @Path("/search") @Consumes(MediaType.APPLICATION_JSON) public Response getSubscriber(@QueryParam("data") SubscriberSearchFormData data){ System.out.println(data); List <SubscrEntity> results = null //list of results return Response.ok(results).build(); } 

SubscriberSearchFormData class:

 public class SubscriberSearchFormData { private String name; private String street; private Integer contractNumber; public static SubscriberSearchFormData fromString(String jsonRepresentation) throws Exception { System.out.println("WE ARE HERE"); ObjectMapper mapper = new ObjectMapper(); // Jackson's JSON marshaller SubscriberSearchFormData obj = null; try { obj = mapper.readValue(decoded, SubscriberSearchFormData.class); } catch (IOException e) { throw new Exception("Wrong JSON parameters!"); } return obj; } //all getters and setters } 

As planned, JSON should be automatically parsed by the fromString method into an object of the SubscriberSearchFormData class. And we will continue to work with him. But as soon as I call the service:

 localhost:8080/application/rest/catalog/subscriber/search?data={ "name":"bbb", "street":"eee", "contractNumber":5 } 

Everything falls due to an error:

 11:24:04,050 WARN [org.jboss.resteasy.resteasy_jaxrs.i18n] (default task-3) RESTEASY002130: Failed to parse request.: javax.ws.rs.core.UriBuilderException: RESTEASY003330: Failed to create URI: http://localhost:8080/application/rest/catalog/subscriber/search?data={%20%22name%22:%22bbb%22,%20%22street%22:%22eee%22,%20%22contractNumber%22:%225%22} 

And at the same time, System.out.println("WE ARE HERE"); not even called. That is, it collapses, even before calling fromString();

I dig in this second day and nothing can be solved.

  • one
    Why can't JSON be transmitted in the request body? Why pass it as a parameter, thereby turning the link into not valid. - Oleg Marchenko
  • @ MoS1993 Could you explain in more detail what do you mean? In the request body, i.e. how to POST? - Kiryl Aleksandrovich
  • one
    and the fromString method should throw an Exception ? - MrFylypenko
  • @MrFylypenko Of course, you should try { obj = mapper.readValue(...); } catch (IOException e) { throw new Exception("Wrong JSON parameters!"); } try { obj = mapper.readValue(...); } catch (IOException e) { throw new Exception("Wrong JSON parameters!"); } try { obj = mapper.readValue(...); } catch (IOException e) { throw new Exception("Wrong JSON parameters!"); } - Kiryl Aleksandrovich

1 answer 1

Install yourself a Chrome browser extension - Postman, which will help in working with HTTP requests.

Implement the method this way:

 @POST @Path("/search") @Consumes(MediaType.APPLICATION_JSON) public Response getSubscriber(SubscriberSearchFormData data) { System.out.println(data); List <SubscrEntity> results = null //list of results return Response.ok(results).build(); } 

The SubscriberSearchFormData class needs to add a default constructor and getters / setters.

Next, send a POST request to the server:

And in the method get your object.

  • Fine! I accept your answer with pleasure. But first tell me: is it possible to do the same, but still with a gett request? It is very necessary through het. - Kiryl Aleksandrovich
  • And why specifically GET? By the name of the URL, it is assumed that there will be POST, because you say to the server: "find me subscribers by the given parameters"! If I'm not mistaken, then the GET request can also be done with the body. - Oleg Marchenko
  • No, I do not agree with you. A POST request is used to send some data to the server, as a rule, one way. For example, sending a form with a user profile. And in my case, this is a search. Find and get. And accordingly, the GET request will be semantically correct. But in any case, thank you very much! - Kiryl Aleksandrovich