Redundancy between @ResponseBody and @PostMapping(path = "/test", consumes =…, produces = MediaType.APPLICATION_JSON_VALUE)?

huangapple go评论124阅读模式
英文:

Redundancy between @ResponseBody and @PostMapping(path = "/test", consumes =..., produces = MediaType.APPLICATION_JSON_VALUE)?

问题

关于 @ResponseBody@XXXMapping([...]) produces = MediaType.APPLICATION_JSON_VALUE 的小问题:

它们是否实现了完全相同的功能?

将这两者放在一起是否多余?

如果不是,它们之间的区别是什么?

如果是,应该优先选择哪个?

将这两者放在一起是否会带来任何积极的效果?

是否有任何使用情况应该同时使用这两者?

谢谢

英文:

Small question regarding

@ResponseBody and @XXXMapping([...]  produces = MediaType.APPLICATION_JSON_VALUE)

Are they achieving the exact same thing?

Having the two together is redundant?

If no, what are the differences?

If yes, which one should be preferred?

Having the two together can bring anything positive? Any use case where we should have the two together?

Thank you

答案1

得分: 1

@ResponseBody通常与@Controller一起使用,用于ajax端点,以让Spring知道您不会呈现HTML页面,而是要返回JSON,或者如果您进行了配置,还可以返回XML等。如果您使用的是@RestController,则不是必需的。这有点类似于这个链接,但我想可能有些不同的地方。🤷‍♀️

英文:

@ResponseBody is generally used with @Controller for an ajax endpoint to let spring know you aren't rendering an html page, you want to return json or if you configure it, xml, etc. If you are using an @RestController it's not necessary. This is sort of a duplicate of this but I guess it's kind of different 🤷‍♀️

答案2

得分: 0

具体来说,@PostMapping是一个组合注解,作为@RequestMapping(method = RequestMethod.POST)的快捷方式。

@ResponseBody注解告诉控制器返回的对象会自动序列化为JSON格式,并传回到HttpResponse对象中。

假设我们有一个自定义的Response对象:

public class ResponseTransfer {
    private String text; 
    
    // 标准的getter和setter方法
}

接下来,相关的控制器可以这样实现:

@Controller
@RequestMapping("/post")
public class ExamplePostController {

    @Autowired
    ExampleService exampleService;

    @PostMapping("/response")
    @ResponseBody
    public ResponseTransfer postResponseController(
      @RequestBody LoginForm loginForm) {
        return new ResponseTransfer("Thanks For Posting!!!");
     }
}

在浏览器的开发者控制台中或使用类似Postman的工具,我们可以看到以下响应:

{"text":"Thanks For Posting!!!"}

记住,对于使用@RestController注解的控制器,不需要再额外添加@ResponseBody注解,因为Spring会默认处理。

现在,让我们实现一个新的方法,映射到相同的路径“/content”,但返回XML内容:

@PostMapping(value = "/content", produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ResponseTransfer postResponseXmlContent(
  @RequestBody LoginForm loginForm) {
    return new ResponseTransfer("XML Content!");
}
英文:

Specifically, @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST)

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.

Suppose we have a custom Response object:

public class ResponseTransfer {
    private String text; 
    
    // standard getters/setters
}
Next, the associated controller can be implemented:

@Controller
@RequestMapping("/post")
public class ExamplePostController {
 
    @Autowired
    ExampleService exampleService;
 
    @PostMapping("/response")
    @ResponseBody
    public ResponseTransfer postResponseController(
      @RequestBody LoginForm loginForm) {
        return new ResponseTransfer("Thanks For Posting!!!");
     }
}

In the developer console of our browser or using a tool like Postman, we can see the following response:

{"text":"Thanks For Posting!!!"}
Remember, we don't need to annotate the @RestController-annotated controllers with the @ResponseBody annotation since Spring does it by default

let's implement a new method, mapped to the same /content path, but returning XML content instead:

@PostMapping(value = "/content", produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ResponseTransfer postResponseXmlContent(
  @RequestBody LoginForm loginForm) {
    return new ResponseTransfer("XML Content!");
}

huangapple
  • 本文由 发表于 2020年10月17日 10:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64398434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定