How to POST a JSON payload to a @RequestParam in Spring MVC
Yes,is possible to send both params and body with a post method:
Example server side:
@RequestMapping(value ="test", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Person updatePerson(@RequestParam("arg1") String arg1,
@RequestParam("arg2") String arg2,
@RequestBody Person input) throws IOException {
System.out.println(arg1);
System.out.println(arg2);
input.setName("NewName");
return input;
}
and on your client:
curl -H "Content-Type:application/json; charset=utf-8"
-X POST
'http://localhost:8080/smartface/api/email/test?arg1=ffdfa&arg2=test2'
-d '{"name":"me","lastName":"me last"}'
Enjoy
Spring Boot using Json as request parameters instead of an entity/model
In that case you can use Map
class to read input json, like
@PostMapping("/create")
public Object create(@RequestBody Map<String, ?> input) {
sout(input.get("param1")) // cast to String, int, ..
}
Spring MVC : post request and json object with array : bad request
I think you're looking for something like `@RequestBody. Create a class to represent your JSON data. In your case, this class will contain two member variables - globalId as a string and lines as an array of the object it represents. Then in your controller method, you will use the @RequestBody annotation on this class type so that Spring will be able to convert the JSON into object. Check the examples below.
http://www.leveluplunch.com/java/tutorials/014-post-json-to-spring-rest-webservice/
JQuery, Spring MVC @RequestBody and JSON - making it work together
http://www.techzoo.org/spring-framework/spring-mvc-requestbody-json-example.html
Can I use @Requestparam annotation for a Post request?
What you are asking for is fundamentally wrong. POST requests sends data in a body payload, which is mapped via @RequestBody
. @RequestParam
is used to map data through the URL parameters such as /url?start=foo
. What you are trying to do is use @RequestParam
to do the job of @RequestBody
.
Alternative solutions for REST controllers
- Introduce a DTO class. It is the most preferred and clean method.
- If you really want to avoid creating a class, you can use
@RequestBody Map<String, String> payload
. Be sure to include'Content-Type': 'application/json'
in your request header. - If you really want to use
@RequestParam
, use a GET request instead and send your data via URL parameters.
Alternative solutions for MVC controllers
- Introduce a DTO class and use it with annotation
@ModelAttribute
. - If you transform the form data into JSON, you can use
@RequestBody Map<String, String> payload
. To do this, please see this answer.
It is not possible to map form data encoded data directly to a Map<String, String>
.
JSON Payload not Parsed on POST Spring 4.x MVC
I don't fully understand your problem because you haven't provided a clear indication of what the problem actually is. That being said, the following should work just fine:
@RestController
class FooController {
@RequestMapping(
value = "/some/endpoint",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public MyResponseObject someEndpoint(@RequestBody MyRequestObject requestBody)
{
MyResponseObject responseObject = new MyResponseObject();
responseObject.setSomeProperty("foo");
responseObject.setAnotherProperty(responseObject.getSomeProperty("bar"));
return responseObject;
}
}
How to access the String json payload and mapping it to an Object in Spring rest controller?
There's many options for you solve. Since you don't want to persist the shouldSendEmail
flag and it's ok to add into you domain class, you can use the @Transient
annotation to tell JPA to skip the persistence.
@Entity
public class Person {
@Id
private Integer id;
@NotNull
private String name;
@NotNull
private String password;
@Transient
private Boolean shouldSendEmail;
}
If you want more flexible entity personalizations, I recommend using DTO`s.
MapStruct is a good library to handle DTO`s
Related Topics
How to Trim White Space from All Elements in Array
Null Pointer on an Autowired Bean Which Is Not Mocked by Mockito
Jpa:How to Convert a Native Query Result Set to Pojo Class Collection
Java: How to Ask User If He/She Wants to Continue Program
How to Change Read-Only Status of Files in Intellij
Tomcat Is Running But 8080 Port Is Not Responding
Error: Java: Invalid Target Release: 11 - Intellij Idea
Springboot Data - No Property Registration Found
Redirecting to a Servlet from a Jsp on Button Click
Why Is My Spring @Autowired Field Null
Java: How to Print Array Without Square Brackets and Commas
Serialize a Double to 2 Decimal Places Using Jackson
Java.Exe Finished With Non-Zero Exit Value 1
Spring Boot: Cannot Access Rest Controller on Localhost (404)