gpt4 book ai didi

java - 无法实例化 PersistentEntityResourceAssembler

转载 作者:行者123 更新时间:2023-12-02 12:04:11 26 4
gpt4 key购买 nike

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

Controller

@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));
}
}

API: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>()

最佳答案

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

并删除PersistentEntityResourceAssembler assembler来自响应正文

关于java - 无法实例化 PersistentEntityResourceAssembler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47028427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com