I wanted to know if theres a way to send a JSON data along with HTTP response code 500. Basically I want my rest client to know that there is some error on the backend and along with it send a JSON error data structure like this.
{"error" : [
{"code": "1001", "desc": "Some error description"},
{"code": "1002", "desc": "Some other error description"}
]
}
This is using the following java stack = Java 6/JAX-RS/Jersey/Tomcat
If not, then is there a way to send a custom response code along with JSON data. Basically looking from JAX-RS API it looks that you can only send JSON data along with 200 OK??
Any thoughts?? I am guessing RESTEasy would be the same, right??
You simply need to set the HTTP header response to whatever you want before starting to output the actual response using something like setStatus(). Response.status(500)
Related
I'm using Java gcm-server to send messages to Android client apps, and I would like to send a message to a specific topic as showed here.
My problem is that I only managed to send a message using the Sender class, to a list of registration_ids, and I can't figure out how to use parameter "to": "/topics/myTopic" (I read the source code and it seems it is not implemented).
Any advice?
Yes,Right Now... Not Implemented yet,
you need to make a HTTP POST request to :
https://android.googleapis.com/gcm/send
with body
{
"data":
{
"title": "Test Title",
"message": " Your Message"
},
"to" : "/topics/global"
}
For more Help -
https://github.com/googlesamples/google-services/blob/master/android/gcm/gcmsender/src/main/java/gcm/play/android/samples/com/gcmsender/GcmSender.java
When I send request it looks like (Google Chrome Console) :
42/v1/user,["currentPerson",{"transactionId":"53445t-53454-534543-53453435"}]
where 53445t-53454-534543-53453435 - is unique transactionId which I automatically include in request and each response.
The response looks like
42/v1/user,["currentPerson","53445t-53454-534543-53453435",{"id":5,"firstName":"Ivan"}]
where /v1/user - namespace with versioning;
1) I don't want the 42. And don't want array on top-level
2) I just want such kind of response
{
"transactionId": "53445t-53454-534543-53453435",
"namespace": "/v1/user",
"event": "currentPerson",
"person": {
"id": 5,
"firstName": "Ivan"
}
}
3) And such kind of request
{
"transactionId": "53445t-53454-534543-53453435",
"namespace": "/v1/user",
"event": "currentPerson"
}
Now I use socket-io js client but only for test purposes, service will be used with using some C++ library, so I need to use not the socket.io style format.
Is it possible? What methods can I use?
I have found addJsonObjectListener method and sendJsonObject, but they was removed in 1.7.0 and anyway I don't want to pass a #class directive, it seems weird to me to give client a knowledge about server class hierarchy. I just want define some object on the client and parse it manually on the server, then pass manually formed json to the client.
P.S.: By the way Configuration.jsonTypeFieldName is not available now, so #class now is the only option of name.
netty-socket-io 1.7.7
I suppose to convert Json response in String and send it as String.
String json = convertInString(json);
buf = Unpooled.copiedBuffer(json, CharsetUtil.UTF_8);
ctx.write(a);
I have been battling with this for a while and cant find a solution. I am running the SpringBoot guides and the Accessing JPA Data (http://spring.io/guides/gs/accessing-data-rest/) with REST is not working.
Just downloading it and running it runs fine allowing a GET call to
http://localhost:8080/people
using POSTMAN. However whenever I try a PUT or POST I get the following error
{
"cause": null,
"message": "No suitable HttpMessageConverter found to read request body into object of type class hello.Person from request with content type of text/plain;charset=UTF-8!"
}
The json I am passing with the PUT is
{
"firstName": "Dave",
"lastName": "Something"
}
This is just running the vanilla project with no changes. Lots of the other guide projects work fine so I know MVN, Spring boot etc are working ok.
Have tried a lot of forums but nothing suggested works.
Any help would be greatly appreciated.
Thanks
Dave
Your POST request has a content type of text/plain and the server doesn't know how to convert plain text into a hello.Person instance.
You need to tell the server that you're sending JSON by setting the Content-Type header of the request to application/json
I used Google Chrome's Postman extension and sent raw JSON { "firstName": "Frodo", "lastName": "Baggins" }. That worked.
Keep in mind to specify the HTTP header Content-Type: application/json.
Content-Type:application/json try to remove the space between content-type:* and application in the header .
Make sure you have selected the
Body->RAW->Json in the Api Client and try it worked for me.
In my java client application, I am accessing a endpoint URL and could able to get response back, but it is in HTML code!.
Method : Post
resource.accept(MediaType.APPLICATION_JSON_TYPE);
WebResource resource = Client.create().resource(
communicatorVO.getTargetURL());
String **response** = resource.queryParams(communicatorVO.getFormData()).type(MediaType.APPLICATION_JSON_TYPE).post(String.class, gson.toJson(communicatorVO.getRequestObject()));
The response object always contains HTML code! How to get the actual data?
If I try using chrome restful client, am getting below response.
{
"access_token" : "YOUR_NEW_ACCESS_TOKEN",
"token_type" : "bearer",
"expires_in" : 10800,
"refresh_token" : "YOUR_REFRESH_TOKEN",
"scope" : "write read offline_access"
}
This issue has been resolved.
I added type & accept in single line and it started returning expected json response. Now I can parse the json into any java object.
Code :
response = resource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).accept(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, communicatorVO.getFormData());;
I am creating a REST API on top of an existing application. One of the features takes in a json data along with a file uploaded by the user.
I am unsure how to send a file AND json data in the same request to the REST API?
I have the json part working and I test that using curl:
curl -XPOST http://localhost:8080/myapp/foo -d '{"mydata": {
"name": "somename",
"gender": "male"
}}'
//I would like to send an image (say, profile image) with the above request as well.
I'm using a grails application so I get this data in my controller like so: new Foo(params.mydata).
Question
Is it possible to send JSON data and a file in the same request to the API? If so, how can I do it using curl or REST Console (chrome extension)
What would be the contentType of this request?
I'm open to sending data in another format if it means that I can send file and other data (strings) within the same request. I'm not tied on JSON
Update
I found another SO question which is asking the same thing. From the answer to that question it seems there are only three choices and none of which say that its possible to send both, json data and file, within the same request. Which is very discouraging...I will keep this question open to see if anyone has other ideas.
I think the "right" way to do this is with a multipart message. That way, you can post up both the JSON and the Image with their corresponding correct MIME type. The wikipedia article on multipart mime types has an example of what this would look like. It looks like both Apache httpcommons and Jersey support this sort of thing, and apparently curl does too!