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
Java.Lang.Noclassdeffounderror: Com/Fasterxml/Jackson/Databind/Exc/Invaliddefinitionexception
Delete Everything After Part of a String
How to Center Crop a Background Image of Linear Layout
Converting Excel File (Xlsx) into Json
How to Efficiently Read Multiple Json Files into a Dataframe or Javardd
How to Use Limit in Spring Within SQL Query
Spring Boot Return 204 No-Content When List Is Empty
Newline Character in Jlabel.Settext()
Parsing the Nested Json Array Using Jackson Library in Java
Adb Cannot Connect to Daemon At Tcp:5037
How to Autowire a Component Which Is Having Constructor With Arguments in Springboot Application
How to Solve Maven 2.6 Resource Plugin Dependency
Ora-00942 Sqlexception With Hibernate (Unable to Find a Table)
Passing Empty List as Parameter to JPA Query Throws Error
Webdriver/Pageobject/Findby: How to Specify Xpath With Dynamic Value