代码之家  ›  专栏  ›  技术社区  ›  SFAH

无法实例化PersistentEntityResourceAssembler

  •  0
  • SFAH  · 技术社区  · 7 年前

    我使用的是spring框架,在下面的代码中,我想获得用户的会话,但当我调用API时,它给了我一个错误详细信息如下

    控制器

    @Controller()
    public class SessionController {
    
        @Autowired UserRepository userRepo;
    
        @RequestMapping(value = "api/session", method = RequestMethod.GET)
        public @ResponseBody ResponseEntity<?> getUserSession(Authentication authentication,
                PersistentEntityResourceAssembler assembler){       
            User user = null;
            if(authentication != null && authentication.getPrincipal() !=null){
                GalaxUser principal = (GalaxUser) authentication.getPrincipal();
                user = userRepo.findByName(principal.getUsername());
            }
            if(user==null){
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
            return ResponseEntity.ok(assembler.toResource(user));
        }
        }
    

    美国石油学会 : http://localhost:8080/api/session

    :

    org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:141)
        at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:101)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    

    实际误差: [org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()

    2 回复  |  直到 7 年前
        1
  •  2
  •   SFAH    7 年前

    好的,现在问题解决了,用下面的语句替换return语句 return new ResponseEntity<User>(user, HttpStatus.OK);

    并删除 PersistentEntityResourceAssembler assembler 来自响应主体

        2
  •  0
  •   Mudassar    7 年前
    1. 第一件你必须使用的东西 @RepositoryRestController 而不是 @Controller .
    2. 使用时 @RepositoryRestController , @ResponseBody 不工作,因此您需要返回 @ResponseEntity 如下所示

      @RequestMapping(value = "api/session", method = RequestMethod.GET) public ResponseEntity<PersistentEntityResource> getUserSession(Authentication authentication, PersistentEntityResourceAssembler assembler){

    对于已编辑的错误堆栈,请自动连接它,而不是将其作为参数传递

      @Autowired
        private PersistentEntityResourceAssembler assembler;