I am a complete beginner at REST services but I need to access some information via REST from a web site. The service has some sample code to show how to login that I have used. The sample code uses Jettison as a JSON parser but when I try to run the following code snippet I get an Exception:
JSONObject post = baseResource.path("login")
.queryParam("service", "ABC").queryParam("auth", authParam)
.accept(MediaType.APPLICATION_JSON_TYPE).post(JSONObject.class);
baseResourse is a WebResource object. The code fails with the following exception:
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException:
A message body reader for Java class org.codehaus.jettison.json.JSONObject, and
Java type class org.codehaus.jettison.json.JSONObject, and MIME media type
application/json; character=utf-8 was not found
The sample code does not suggest that I should need to add any "message body readers" to handle the response? Or do I need to add or do anything obvious to parse the response? Thanks.
You need to include jersey-json module on your classpath. See http://jersey.java.net/nonav/documentation/latest/chapter_deps.html#d4e1817
Related
I am creating a java server with Core java and want to create an Document object. Currently I am using InputStreamReader. I am already entring xml into the request body, Please tell me how to store the request body in The Document obj using core java, No frameworks
I am running a Java Servlet webserver which receives the following request:
http://localhost:8080/ForVen/Recebimento/recebeDispositivo.jsp?lista={"dispositivo":[{"Id":0,"DataMod":"2021-09-28T16:55:55.3528819-03:00","SeSincronizar":0,"NrVersaoReg":1,"DataSincronizacao":"2021-09-28T16:55:55.3538812-03:00","Guid":"BFEBFBFF000206A7","TipoDispositivo":0,"URL":"","VendedorId":1}]}
When I send it, it gives me the following error:
javax.servlet.ServletException: org.json.JSONException: Missing value at character 1 of {"dispositivo":[{"Id":0,"DataMod":"2021-09-28T16:55:55.3528819-03:00","SeSincronizar":0,"NrVersaoReg":1,"DataSincronizacao":"2021-09-28T16:55:55.3538812-03:00","Guid":"BFEBFBFF000206A7","TipoDispositivo":0,"URL":"","VendedorId":1}]}
If I try to send the same request, but with the JSON "formatted", it works.
http://localhost:8080/ForVen/Recebimento/recebeDispositivo.jsp?lista={"dispositivo":[{"Id":0,"DataMod":"2021-09-28T16:55:55.3528819-03:00","SeSincronizar":0,"NrVersaoReg":1,"DataSincronizacao":"2021-09-28T16:55:55.3538812-03:00","Guid":"BFEBFBFF000206A7","TipoDispositivo":0,"URL":"","VendedorId":1}]}
This is the way I am deserializing the JSON:
JSONObject jso = new JSONObject(myIncomingJson);
I don't know if it has something to do with some Apache configuration, but I hope so, it would be extremely painful to change all of the client-side requests.
As f1sh said, I have to encode the JSON that is being send on the URL, my mistake that I didn't saw it. :)
This is how the URL should be:
http://192.168.1.58:8080/ForVen/Recebimento/recebeDispositivo.jsp?lista=%7B%5C%22dispositivo%5C%22%3A%5B%7B%5C%22Id%5C%22%3A0%2C%5C%22DataMod%5C%22%3A%5C%222021-09-30T09%3A15%3A37.4138271-03%3A00%5C%22%2C%5C%22SeSincronizar%5C%22%3A0%2C%5C%22NrVersaoReg%5C%22%3A1%2C%5C%22DataSincronizacao%5C%22%3A%5C%222021-09-30T09%3A15%3A37.4148274-03%3A00%5C%22%2C%5C%22Guid%5C%22%3A%5C%22BFEBFBFF000206A7%5C%22%2C%5C%22TipoDispositivo%5C%22%3A0%2C%5C%22URL%5C%22%3A%5C%22%5C%22%2C%5C%22VendedorId%5C%22%3A1%7D%5D%7D
I have a UI page developed using angular js and make a rest webservice call. If an exception is thrown by webservice will the control be returned to the UI with the error?
yes, it will be returned to the client. This is true even if the server timesouts.
But it might not be in a format as you expected. For example, you might have sent a json request and expecting a json response. But you might get an html response if the server has setup a simple html handler for error code. In this case your response parsing will fail. One common example is the authentication failure, which simply redirects you to html login page in many services.
So before parsing the result, check the status code. Check the service rest api docs and study the error handling semantics. Then parse the error accordingly.
My web clients send GET requests with URL query parameters. The receiving App can only accept POST requests with JSON body. I would like to embed a jetty servlet to the receiving App which converts GET requests to POST request, with url parameters being converted to json format body.
Input GET url for example: http://localhost:8081/?key_1=value_1&key_2=3value_2...&key_n=value_n
Expected POST json payload: {"key_1":"value_1", "key_2":"value_2", ..."key_n":"value_n"}
Could you please illustrate how to implement such functions?
I`ve worked with other programming languages, but am completely new to java. I really really appreciate your help.
Thanks and best regards,
Fischlein
You can read all the query string parameter and put it into HashMap. Then serialize this hashmap using jackson-json api or google gson api.
Jackson Reference Url :
http://wiki.fasterxml.com/JacksonHome
Read the parameters from the get request, create a json string and post it with a utility library like http://hc.apache.org/httpclient-3.x/
I want to send a POST request to test my local webservice. The request body is an XML body and i generated the classes using JAXB.
Everything is fine but when I execute the following code
ClientResponse response = service.path("api")
.path("v1")
.path("shipments")
.path("package")
.path("order")
.path("status")
.accept(MediaType.APPLICATION_XML)
.post(ClientResponse.class, orderStatusRequest);
I get the following error
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.api.shipments.model.OrderStatusRequestType, and MIME media type, application/octet-stream, was not found
Can someone point me to a direction, what i am missing here?