问题
有人能解释一下Spring 3中的@RequestBody
和@ResponseBody
注释吗?它们适用于什么?任何例子都会很棒。
#1 热门回答(177 赞)
文档中有一整节称为16.3.3.4 Mapping the request body with the @RequestBody annotation。一个叫16.3.3.5 Mapping the response body with the @ResponseBody annotation。我建议你查阅这些部分。也相关:@RequestBody
javadocs,@ResponseBody
javadocs
用法示例如下:
使用像JQuery这样的JavaScript库,你会发布一个像这样的JSON-Object:
{ "firstName" : "Elmer", "lastName" : "Fudd" }
你的控制器方法如下所示:
// controller
@ResponseBody @RequestMapping("/description")
public Description getDescription(@RequestBody UserStats stats){
return new Description(stats.getFirstName() + " " + stats.getLastname() + " hates wacky wabbits");
}
// domain / value objects
public class UserStats{
private String firstName;
private String lastName;
// + getters, setters
}
public class Description{
private String description;
// + getters, setters, constructor
}
现在,如果你的类路径中有Jackson(并且有一个<mvc:annotation-driven>
setup),Spring会将传入的JSON转换为post体的UserStats对象(因为你添加了@RequestBody
注释),它会将返回的对象序列化为JSON(因为你添加了@ResponseBody
注释)。所以浏览器/客户端会看到这个JSON结果:
{ "description" : "Elmer Fudd hates wacky wabbits" }
请参阅我之前的答案以获得完整的工作示例:https://stackoverflow.com/a/5908632/342852
注意:RequestBody / ResponseBody当然不限于JSON,它们都可以处理多种格式,包括纯文本和XML,但JSON可能是最常用的格式。
**更新:**自Spring 4.x以来,你通常不会使用@ResponseBody
的方法级别,而是使用类别级别的282213235,具有相同的效果。 SeeCreating REST Controllers with the @RestController annotation
#2 热门回答(18 赞)
@RequestBody:指示方法参数的注释应绑定到HTTP请求的主体。
例如:
@RequestMapping(path = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
@ResponseBodyannotation可以放在一个方法上,并指示返回类型应直接写入HTTP响应主体(而不是放在模型中,或解释为视图名称)。
例如:
@RequestMapping(path = "/something", method = RequestMethod.PUT)
public @ResponseBody String helloWorld() {
return "Hello World";
}
或者,我们可以用@RestController注释代替88670504注释。这将消除使用@ResponseBody
的需要。
for more details
#3 热门回答(0 赞)
package com.programmingfree.springshop.controller;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.programmingfree.springshop.dao.UserShop;
import com.programmingfree.springshop.domain.User;
@RestController
@RequestMapping("/shop/user")
public class SpringShopController {
UserShop userShop=new UserShop();
@RequestMapping(value = "/{id}", method = RequestMethod.GET,headers="Accept=application/json")
public User getUser(@PathVariable int id) {
User user=userShop.getUserById(id);
return user;
}
@RequestMapping(method = RequestMethod.GET,headers="Accept=application/json")
public List<User> getAllUsers() {
List<User> users=userShop.getAllUsers();
return users;
}
}
在上面的例子中,他们将显示所有用户和特定的id细节,现在我想同时使用id和name,
1)localhost:8093 / plejson / shop / user <---此链接将显示所有用户详细信息
2)localhost:8093 / plejson / shop / user / 11 <----如果我在链接方式中使用11,它将显示特定用户11的详细信息
现在我想同时使用id和name
localhost:8093 / plejson / shop / user / 11 / raju <-----------------喜欢这个意味着我们可以使用任何一个,请帮帮我.... 。