所以我有一个Spring RestController,我的一个端点用于对传递给我的RequestBody的泛型类型对象执行操作,如下所示:
@PostMapping("/endpoint")
public <T extends Comparable<T>> ResponseEntity<Integer> balancingPost(@RequestBody MyCustomObject<T> mco)
因此,经过大量搜索后,如果没有在某个时刻明确说明类型,就无法做到这一点。然而,因为它,我的控制器无法知道类型(虽然调用POST的程序)。那我该怎么处理呢?有没有办法发布我的T类,并以某种方式映射它?
答案 0 :(得分:0)
尝试以下
public ResponseEntity<?> balancingPost(@RequestBody MyCustomObject<T> mco) {
ResponseEntity<?> response = null;
try {
/*Some condition*/
if (!auth.equals(authCode)) {
response = new ResponseEntity<>("Unauthorized", HttpStatus.UNAUTHORIZED);
} else {
MyModel model = service.getModel();
response = new ResponseEntity<>(model, HttpStatus.OK);
}
} catch (Exception ex) {
response = new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
ex.printStackTrace();
}
return response;
}