我们可以在Spring Boot中基于​​内容类型重载api吗?

时间:2020-06-09 16:02:50

标签: java spring spring-boot api rest

我要求Api消耗URL编码的数据和Application JSON都在相同的端点上以不同的方式表现

1 个答案:

答案 0 :(得分:0)

不,它不能那样工作 它将根据请求映射工作,例如获取可选的更新更新等

但是您可以像这样调整和完成工作

//Post method #1
@RequestMapping(method=RequestMethod.POST, value="/foo", headers="returnType=withoutcertainformat")
public @ResponseBody Foo updateFoo(@RequestBody Foo foo) {
    fooService.update(foo);
}

//Post method #2
@RequestMapping(method=RequestMethod.POST, value="/foo", headers="returnType=withcertainformat")
public @ResponseBody FooExtra updateFoo(@RequestBody FooExtra fooExtra) {
    fooService.update(fooExtra);
}