Can someone explain the @RequestBody
and @ResponseBody
annotations in Spring 3? What are they for? Any examples would be great.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Below is an example of a method in a Java controller.
By using @RequestBody annotation you will get your values mapped with the model you created in your system for handling any specific call. While by using @ResponseBody you can send anything back to the place from where the request was generated. Both things will be mapped easily without writing any custom parser etc.
@RequestBody : Annotation indicating a method parameter should be bound to the body of the HTTP request.
For example:
@ResponseBody annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).
For example:
Alternatively, we can use @RestController annotation in place of
@Controller
annotation. This will remove the need to using@ResponseBody
.for more details
In the above example they going to display all user and particular id details now I want to use both id and name,
1) localhost:8093/plejson/shop/user <---this link will display all user details
2) localhost:8093/plejson/shop/user/11 <----if i use 11 in link means, it will display particular user 11 details
now I want to use both id and name
localhost:8093/plejson/shop/user/11/raju <-----------------like this it means we can use any one in this please help me out.....
There is a whole Section in the docs called 16.3.3.4 Mapping the request body with the @RequestBody annotation. And one called 16.3.3.5 Mapping the response body with the @ResponseBody annotation. I suggest you consult those sections. Also relevant:
@RequestBody
javadocs,@ResponseBody
javadocsUsage examples would be something like this:
Using a JavaScript-library like JQuery, you would post a JSON-Object like this:
Your controller method would look like this:
Now if you have Jackson on your classpath (and have an
<mvc:annotation-driven>
setup), Spring would convert the incoming JSON to a UserStats object from the post body (because you added the@RequestBody
annotation) and it would serialize the returned object to JSON (because you added the@ResponseBody
annotation). So the Browser / Client would see this JSON result:See this previous answer of mine for a complete working example: https://stackoverflow.com/a/5908632/342852
Note: RequestBody / ResponseBody is of course not limited to JSON, both can handle multiple formats, including plain text and XML, but JSON is probably the most used format.
Update: Ever since Spring 4.x, you usually won't use
@ResponseBody
on method level, but rather@RestController
on class level, with the same effect. See Creating REST Controllers with the @RestController annotation