BAT*_*008 0 java spring json content-type spring-boot
我有一个将由前端调用的 Spring-Boot 控制器应用程序。Spring-boot@PostMapping将接受XML和JSON。我想根据Content-Type.
有没有办法检查传入的内容类型是什么?
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api")
public class MyController {
@PostMapping(value = "/generator", consumes = {"application/json", "application/xml"}, produces = "application/json")
public String generate(@RequestBody String input) {
try {
System.out.println("INPUT CONTENT TYPE : ");
if(contentType == "application/xml")
{
//Call Method-1
}else if(contentType == "application/json"){
//Call Method-2
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如我们所看到的,该RestController方法接受XML and JSON. 我想检查传入的内容Content-type是根据其需要做出不同的决定。有人可以向我解释一下该怎么做吗?
请注意:我知道我可以创建不同的方法来处理 XML 和 JSON,但我想用单一方法来完成,这样既简单又高效。
添加RequestHeader及其名称Content-type:
public String generate(@RequestBody String input, @RequestHeader("Content-type") String contentType)
Run Code Online (Sandbox Code Playgroud)
指示方法参数应绑定到 Web 请求标头的注释。
| 归档时间: |
|
| 查看次数: |
2335 次 |
| 最近记录: |