Pass and Get Json String to Spring Controller

Trying to use Spring Boot REST to Read JSON String from POST

I think the simplest/handy way to consuming JSON is using a Java class that resembles your JSON: https://stackoverflow.com/a/6019761

But if you can't use a Java class you can use one of these two solutions.

Solution 1: you can do it receiving a Map<String, Object> from your controller:

@RequestMapping(
value = "/process",
method = RequestMethod.POST)
public void process(@RequestBody Map<String, Object> payload)
throws Exception {

System.out.println(payload);

}

Using your request:

curl -H "Accept: application/json" -H "Content-type: application/json" \
-X POST -d '{"name":"value"}' http://localhost:8080/myservice/process

Solution 2: otherwise you can get the POST payload as a String:

@RequestMapping(
value = "/process",
method = RequestMethod.POST,
consumes = "text/plain")
public void process(@RequestBody String payload) throws Exception {

System.out.println(payload);

}

Then parse the string as you want. Note that must be specified consumes = "text/plain" on your controller.
In this case you must change your request with Content-type: text/plain:

curl -H "Accept: application/json" -H "Content-type: text/plain" -X POST \
-d '{"name":"value"}' http://localhost:8080/myservice/process

400 (Bad Request) on passing JSON to Spring controller

You need to enclose OuterCover class into other class as the request has OuterCover element. E.g.

class RequestDto{

@JsonElement("outerCover")
private OuterCover outerCover;

//getters and setters
}

Alternatively, you can modify the request payload and remove 'outerCover' element e.g.:

[{
"id": 123456,
"name": "First Item"
},
{
"id": 78910,
"name": "Second Item"
}]

Spring MVC - How to return simple String as JSON in Rest Controller

Either return text/plain (as in Return only string message from Spring MVC 3 Controller) OR wrap your String is some object

public class StringResponse {

private String response;

public StringResponse(String s) {
this.response = s;
}

// get/set omitted...
}



Set your response type to MediaType.APPLICATION_JSON_VALUE (= "application/json")

@RequestMapping(value = "/getString", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)

and you'll have a JSON that looks like

{  "response" : "your string value" }

Passing json as body and another object as parameter to spring controller

There are several ways to do this. The first option is to remove the @RequestParam annotation and Spring will automatically bind the parameters in the query string to your POJO. However, in your code you are appending a JSON representation to the URL, and that's not possible. This is the correct syntax you should be using for the AddDetails fields:

/addstock?firstname=value1&lastname=value2

Another possible solution is to define a DTO that contains both the list of stocks and an AddDetails object and pass it as body of the request. For example:

public class StockListDTO{

List<Stock> stocks;

AddDetails details;

//Getter and setters or use Lombok

}

And your method signature:

 @RequestMapping(value = "/addstock", method = RequestMethod.POST)
public @ResponseBody
String addPersons(@RequestBody StockListDTO body) throws ParseException, IOException


Related Topics



Leave a reply



Submit