I am building project REST project with Spring Boot and i come across an issue of sending JSON from client to server.
My Scenerio is i want to send json like this using postman REST client:
{
"test":"success"
}
And want to get this json using this method:
#RequestMapping(value = "/user", method = RequestMethod.POST)
public Map<String, Object> postData(#RequestBody Map map){
log.info("in test Connection method");
return map;
}
I am using above method but it is giving exception.
If it is not possible to process json data with #RequestBody with POST request then is there any other way to get json data with POST request and process that json data?
I have just tested it here and it works fine.
You have to specify the Content-Type header in your POST request though, and set it to application/json. You can easily do this in Postman in the Headers tab.`
Without it you will most likely get an Internal Server Error (500) saying
Content type 'text/plain;charset=UTF-8' not supported
Related
I am using Jersey Rest Service. I am getting clients requests in json and getting java object out of it. Everything works fine. However, is there anyway I can get the exact json that was pass from client without even converting to java object.
Issue is json request contents just two parameters and below ObjectMapper converts back to Json but with null values. To ignore, I have to put #JsonInclude(Include.NON_NULL) on each pojo class. If I can get just client json, then it would be good.
ObjectMapper mapper=new ObjectMapper();
String jsonString= mapper.writeValueAsString(body);
Keep the following in your resource method:
#Consumes("application/json")
public Response fn(String payload){} //Notice the payload of type string.
That way the payload will be just a String. I am still not sure why you want it this way, but it should work.
I am sending image as base64_encoded in JSON_PAYLOAD from front-end AJAX request, on the other side I am using LUMEN API to accept that request, get file and store it in db in field having LONGBLOG type, but problem is that when I dump my API method to see the payload sent by front-end it dumps nothing.
my payload is:
{
data:example_base_64_encoded_file
}
here is api method.
public function upload(Request $request){
dd(request->data);
}
It gives error that calling the undefined object and when dump only dd($request) it dumps null
CORS are already enabled. I also tried to enable content-type as base_64.
I am using Postman to send a request to the Zuul proxy. Zuul proxy is running on the port 8085
(http://localhost:8085/helloworld)
I am having a zuul filter and calling a micro service from the zuul filter using Spring RestTemplate. Micro service is running on 8089
ResponseEntity<String> response = restTemplate.exchange(
"http://localhost:8089/helloworld", HttpMethod.GET, entity, String.class);
When I get a successful response from the microservice, I am receiving a successful JSON object response in Postman.
But when an error/exception is returned from the microservice, the zuul filter does not send it back it to the calling client. I am getting an empty body with httpstatus message 200 in Postman, even though microservice returned a 500 error.
I tried to send the microservice response using
ctx.setResponseBody(response.getBody());
Basically this will convert the JSON response from the micro service into a String. I was able to get the response as a String object in the Postman.
Could anyone help me how to get the error/exception response as JSON object from the Zuul filter.
I am new to RESTful web services. Can i have a method like below which accepts JSON as object ?
#GET
#Produces("application/json")
#Consumes("application/json")
#Path("/test")
public Response modifyJson(JSONObject input);
I believe that by default it is not possible to send Json as input to a GET, only other verbs (POST, PUT...)
Having said that, technically it depends of the service to allow you or not to send input on GET. The usual is to send query string parameters with GET.
Hope it helps
Ademar
I'm trying to send JSON data to a server.
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
builder.setHeader("Content-Type", "application/json");
builder.setRequestData(getJSONString());
builder.setCallback(myCallbackObject);
builder.send();
I do this in eclipse and I saw in the TCP/IP Monitor that my JSON Data is not transmitted as post in the request. If I ask for
builder.getRequestData();
I can see the JSON data is right there.
What do I need for the data to get on the server?
You might be running into the browser's same-origin policy if the url that you are attempting to connect to is not from the same origin as your GWT nocache.js file. Is your callback's onFailure() being called? Also, see if Request.getSatusCode() returns 0, which is indicative of SOP problems.
To simply post JSON data with HTTP post, you can use GWT Overlay Types to define a post method using JQuery. A possible snapshot:
public static native void post(String json) /*-{
var data = JSON.parse(json);
$wnd.$.post(url, data);
}-*/;
I don't know if it is necessary to add JQuery to your HTML. I did, and worked. You can do this by adding the following line to your .html file:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Then, you can simply call that method in your java code:
post(json);
(: