我正在使用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));
}
}
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>()
答案 0 :(得分:1)
好的,现在通过用以下语句替换return语句来解决问题
return new ResponseEntity<User>(user, HttpStatus.OK);
并从回复正文中删除PersistentEntityResourceAssembler assembler
答案 1 :(得分:0)
@RepositoryRestController
而不是@Controller
。使用@RepositoryRestController
时,@ResponseBody
无效,因此您需要返回@ResponseEntity
,如下所示
@RequestMapping(value = "api/session", method = RequestMethod.GET)
public ResponseEntity<PersistentEntityResource> getUserSession(Authentication authentication, PersistentEntityResourceAssembler assembler){
编辑: 对于编辑的错误堆栈,请将其自动装配,而不是将其作为参数传递
@Autowired
private PersistentEntityResourceAssembler assembler;