我正在对 CRUD 进行抽象,使用我需要的方法创建一个抽象类。虽然我已经完成了它,但是当我发送插入(POST)和更新(PUT)请求时,Spring框架不会将 <@RequestBody T> 转换为具体类。任何调用“createdAction”或“updateAction”的方法都不起作用。
@RestController
@RequestMapping("/user")
public class UserWebResource extends AbstractWebResource<UserEntity> { }
__
public abstract class AbstractWebResource<T extends PersistenceEntity> {
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<T> createAction(@RequestBody T dataEntity, HttpSession session) {
dataEntity = rule.save(dataEntity);
if (dataEntity.hasErrors()) {
return new ResponseEntity<T>(dataEntity, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<T>(dataEntity, HttpStatus.CREATED);
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = "application/json")
public ResponseEntity<T> updateAction(@PathVariable("id") Integer id, @RequestBody T dataEntity, HttpSession session) { ... }
}
POST 和 PUT 错误
GRAVE:路径 [/app] 上下文中 servlet [dispatcher] 的 Servlet.service() 引发异常 [请求处理失败;嵌套异常是 java.lang.IllegalStateException:参数类型不匹配 处理程序方法详细信息: Controller [br.inf...web.resource.UserWebResource] 方法[public org.springframework.http.ResponseEntity br.inf...web.resource.AbstractWebResource.updateAction(java.lang.String,T,javax.servlet.http.HttpSession)]
最佳答案
编码(marshal)框架似乎存在问题。
您不能在 Jackson 2.7 和 Spring 4.2 中使用泛型类型,您应该将 Jackson 依赖版本回滚到 2.6.6 或使用 Spring 4.3(当它发布时)。
关于java - 抽象类 @RestController Spring 4 - @ResponseBody 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334091/