Sending POST request to a webservice - java

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?

Related

how to insert body in a post request with akka library using java

have to use akka library with java and i need to do a post request with body in it, a body like this: {"subjectId":961,"other":null}
In this application, I have some examples already written with get requests and parameters to those requestes. but now i need to send a post request with body in it.
The following snippet is correct? if not, can you tell me what i do wrong? thanks.
..
Http http = new Http((ExtendedActorSystem) context().system());
HttpRequest postRequest = HttpRequest.POST(url)
.withEntity(MediaTypes.APPLICATION_JSON.toContentType(),
"{\"subjectId\": 961, \"other\": null,}");
CompletionStage<HttpResponse> response = http.singleRequest(postRequest);
..

Jersey 2 REST Client - Read Multipart Response / OctetStream Response

I'm building a Jersey 2 client, which calls a service to get a file from the server.
The service returns binary file content as application/octet-stream
NOw, this is my code where I call the webservice
Response response = target.request().header(HttpHeaders.COOKIE, this.cookie)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM)
.accept(MediaType.APPLICATION_OCTET_STREAM).get();
I get a HTTP 200 Response. But i don't understand how I can get the file content from the response.
EDIT
The service documentation says "To GET the binary file content and the metadata, use header Accept: multipiart/mixed"
So, I tried the below
Response response = target.request()
.header(HttpHeaders.COOKIE, this.cookie)
.header(HttpHeaders.CONTENT_TYPE, "multipart/mixed")
.accept("multipart/mixed").get();
Even here, I get a HTTP status 200 response. But How do I read the file content??
Please help!!
Take a look at the documentation you will see that response has a readEntity method that you can use to read the inputstream:
InputStream in = response.readEntity(InputStream.class);
... // Read from the stream
in.close();

Making a POST request using Camel

I have a camel route configured for reading from a JMS queue and POST it to a service.
My route is :
from("jms:queue")
.marshal()
.json(JsonLibrary.GSON)
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.process(new Processor1())
.to("https4:xxxxxx?throwExceptionOnFailure=false")
.process(new MyProcessor())
My Configuration :
HttpComponent httpc = getContext().getComponent("http4",HttpComponent.class);
httpc.setHttpConfiguaration(customHttpConfig())
and in customHttpConfig i set my auth details.
I am getting a 400 error from the server. But i am able to hit the server from Postman api and get successfull response.
In my Processor1 class ( before making the request), i am able to print the body of the message, which contains a json representation of my object.
But in the processor after the POST request, i am doing this and getting below responses:
Message in = exchange.getIn();
in.getBody(String.class); // print the error code 400
HttpServletRequest request = in.getBody(HttpServletRequest.class)// This is null.
Anything i am doing wrong? Do i need to set the message Content to the body of my Post Request?
It will not be String.class:
in.getBody(String.class);
First check the type of server response, then try to print or log exchange body.
It should be HttpServletResponse:
HttpServletRequest request = in.getBody(HttpServletRequest.class)

Getting application/json back from a Solr query

I'm calling the Solr REST api using a Jersey client:
final ClientResponse resp =
client().path(queryPath())
.queryParam("q", query.getQuery())
.queryParam("wt", "json")
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(ClientResponse.class);
resp.getEntity(HttpResponse.class)
and when I run it I get:
A message body reader for Java class challenger.HttpResponse, and Java type class challenger.HttpResponse, and MIME media type text/plain; charset=UTF-8 was not found
Which suggests to me that the response is 'text/plain' rather than the 'application/json' requested.
Is it possible to get a real JSON response from a solr get query or do I need to marshal the JSON returned as text into an object myself?
The wt param, should take care of JSON response format, as per this. However, things can go wrong sometimes, as mentioned like JSON responses can be returned as plain text, with a change in solrconfig.xml. Please check that option also. Hope this helps you in identifying the issue.

Java Jersey Jettison Message Body Reader Exception

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

Categories