抽象类@RestController Spring 4 - @ResponseBody错误

时间:2016-03-31 12:53:33

标签: java spring spring-mvc

我正在对我的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 e PUT错误

GRAVE:Servlet [dispatcher]的Servlet.service()在路径[/ app]的上下文中引发异常[请求处理失败;嵌套异常是java.lang.IllegalStateException:参数类型不匹配 HandlerMethod详细信息: 控制器[br.inf ... web.resource.UserWebResource] 方法[public org.springframework.http.ResponseEntity br.inf ... web.resource.AbstractWebResource.updateAction(java.lang.String,T,javax.servlet.http.HttpSession)]

1 个答案:

答案 0 :(得分:0)

好像编组框架存在问题。

您不能在Jackson 2.7&amp ;;中使用泛型类型。在Spring 4.2中,您应该将Jackson依赖版本回滚到2.6.6或使用Spring 4.3(当它出来时)。

source