I have recently updated my web page from whatever was current in 2014 to whatever is current now. I am getting an intermittent error which occurs sometimes when I got to my login page after redeploying, get this error:
The HttpServletRequest attribute must contain an HttpServletResponse for the attribute javax.servlet.http.HttpServletResponse}]
java.lang.IllegalArgumentException: The HttpServletRequest attribute must contain an HttpServletResponse for the attribute javax.servlet.http.HttpServletResponse
at org.springframework.security.web.csrf.LazyCsrfTokenRepository.getResponse(LazyCsrfTokenRepository.java:92) ~[spring-security-web-5.3.13.RELEASE.jar:5.3.13.RELEASE]
at org.springframework.security.web.csrf.LazyCsrfTokenRepository.wrap(LazyCsrfTokenRepository.java:84) ~[spring-security-web-5.3.13.RELEASE.jar:5.3.13.RELEASE]
at org.springframework.security.web.csrf.LazyCsrfTokenRepository.generateToken(LazyCsrfTokenRepository.java:58) ~[spring-security-web-5.3.13.RELEASE.jar:5.3.13.RELEASE]
at com.victoreda.playground.controllers.BaseController.getCsrfToken(BaseController.java:317) ~[classes/:?]
at com.victoreda.playground.controllers.SecurityController.login(SecurityController.java:69) ~[classes/:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.13.jar:5.3.13]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) [spring-webmvc-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) [spring-webmvc-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.3.13.jar:5.3.13]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.3.13.jar:5.3.13]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) [tomcat9-servlet-api.jar:?]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.3.13.jar:5.3.13]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [tomcat9-servlet-api.jar:?]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat9-websocket-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:209) [spring-security-web-5.3.13.RELEASE.jar:5.3.13.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178) [spring-security-web-5.3.13.RELEASE.jar:5.3.13.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358) [spring-web-5.3.13.jar:5.3.13]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271) [spring-web-5.3.13.jar:5.3.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.filters.ExpiresFilter.doFilter(ExpiresFilter.java:1227) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.3.13.jar:5.3.13]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.3.13.jar:5.3.13]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:747) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat9-catalina-9.0.31.jar:9.0.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat9-coyote-9.0.31.jar:9.0.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat9-coyote-9.0.31.jar:9.0.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat9-coyote-9.0.31.jar:9.0.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639) [tomcat9-coyote-9.0.31.jar:9.0.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat9-coyote-9.0.31.jar:9.0.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat9-util-9.0.31.jar:9.0.31]
at java.lang.Thread.run(Thread.java:829) [?:?]
The java code that handles(?) my login page is:
#RequestMapping(value = "/loginpage", method = RequestMethod.GET)
public ModelAndView login(HttpServletRequest request, #RequestParam(value = "error", required = false) String error) {
final String methodName = "login";
Helper.logEntrance(getLogger(), methodName);
ModelAndView result = new ModelAndView(methodName);
if (error != null) {
result.addObject("error", "Invalid username or password");
}
result.addObject("_csrf", getCsrfToken(request));
Helper.logExit(getLogger(), methodName, result);
return result;
}
And the code for getCsrfToken is
protected CsrfToken getCsrfToken(HttpServletRequest request) {
// Populate CSRF token since it is not automatically populated by Spring Security
CsrfToken token = getCsrfTokenRepository().loadToken(request);
if (token == null) {
token = getCsrfTokenRepository().generateToken(request);
}
getCsrfTokenRepository().saveToken(token, request, null);
return token;
}
Looking at the Spring source code for generateToken I see that the error is caused here
private HttpServletResponse getResponse(HttpServletRequest request) {
HttpServletResponse response = (HttpServletResponse)request.getAttribute(HTTP_RESPONSE_ATTR);
if (response == null) {
throw new IllegalArgumentException("The HttpServletRequest attribute must contain an HttpServletResponse for the attribute " + HTTP_RESPONSE_ATTR);
} else {
return response;
}
}
because response is (occasionally) null and, despite working on this for about 3 days, I have no idea why. My feeling is that I am missing some annotation or something (because I have upgraded the .jar files).
I'm able to insert data in my table but getting error in findAll api at return statement .
I'm also not able to debug my project. After debugging throw new IllegalArgumentException("null KeyStore name"); exception like in img
#RestController
#CrossOrigin
#RequestMapping("/queryconditionapi")
public class QueryConditionController {
#SuppressWarnings("rawtypes")
#Autowired
QueryConditionService queryConditionService;
#RequestMapping(value = "/allConditions", produces = "application/json", method =
RequestMethod.POST)
public BaseResponse findAll(#RequestParam Integer page, #RequestParam Integer numberofRecords,
#RequestBody QueryConditionRequest queryConditionRequest,#RequestParam Boolean pagination) {
return queryConditionService.getAll(page, numberofRecords, queryConditionRequest, pagination) ;
}
}
this is the error I got after I hit findAll API
in text form
java.lang.IllegalArgumentException: probe is marked non-null but is null
at org.springframework.data.domain.TypedExample.<init>(TypedExample.java:33)
at org.springframework.data.domain.Example.of(Example.java:39)
at com.equaze.service.impl.BaseServiceImpl.getAllData(BaseServiceImpl.java:170)
at com.equaze.service.impl.BaseServiceImpl.getAll(BaseServiceImpl.java:148)
at com.equaze.controller.QueryConditionController.findAll(QueryConditionController.java:46)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.equaze.config.MyCORSFilter.doFilter(MyCORSFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
I've been getting into Java again after some years only dealing with javascript and typescript, and I'm trying to get into Spring Boot.
It sounds really fun and convenient, but I'm trying to do something simple and for some reason I can't get my repository to get injected into my service. It sounds something really simple, but I've been digging for hours and can't get it to work.I'm wondering if some of you can help me to understand what I'm doing wrong ,please.
Thanks in advance
Projects (Entity)
package com.example.demo.modules.projects.entities;
import javax.persistence.*;
#Entity
#Table
public class Projects {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column
private String project_name;
#Column
private String description;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getProject_name() {
return project_name;
}
public void setProject_name(String project_name) {
this.project_name = project_name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
The Repository
package com.example.demo.modules.projects.repositories;
import com.example.demo.modules.projects.entities.Projects;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface IProjectRepository extends JpaRepository<Projects, Long> {
}
CreateProjectsService
package com.example.demo.modules.projects.services;
import com.example.demo.modules.projects.entities.Projects;
import com.example.demo.modules.projects.repositories.IProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class CreateProjectService {
#Autowired
IProjectRepository iProjectRepository;
public Projects execute(Projects project){
System.out.println("Received " + project.toString());
Projects CreatedProject= this.iProjectRepository.save(project);
System.out.println("Created " + CreatedProject);
return CreatedProject;
}
}
The Error
It seems to not be injecting/instantiating correctly.
2020-10-09 00:56:08.774 ERROR 5856 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "com.example.demo.modules.projects.repositories.IProjectRepository.save(Object)" because "this.iProjectRepository" is null] with root cause
java.lang.NullPointerException: Cannot invoke "com.example.demo.modules.projects.repositories.IProjectRepository.save(Object)" because "this.iProjectRepository" is null
at com.example.demo.modules.projects.services.CreateProjectService.execute(CreateProjectService.java:17) ~[classes/:na]
at com.example.demo.ServerApplication.newEmployee(ServerApplication.java:26) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.38.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.38.jar:9.0.38]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
The issue is because you did not allow Spring to instantiate CreateProjectService and takes control of the bean, probably you instantiated the object with new CreateProjectService().
The recommended approach is to create an interface e.g. ICreateProjectService like below:
public interface ICreateProjectService {
Projects execute(Projects project);
}
And then implement ICreateProjectService in CreateProjectService like below:
#Service
public class CreateProjectService implements ICreateProjectService {
#Autowired
private IProjectRepository iProjectRepository;
#Override
public Projects execute(Projects project) {
System.out.println("Received " + project.toString());
Projects CreatedProject= this.iProjectRepository.save(project);
System.out.println("Created " + CreatedProject);
return CreatedProject;
}
}
Then in your Controller layer, as an example of the service usage, autowire your service by injecting the interface, like below:
#Restcontroller
#RequestMapping("/api/v1")
public class CreateProjectRest {
#Autowired
private ICreateProjectService iCreateProjectService;
#PostMapping
public Projects execute(Projects project) {
createProjectService.execute(project);
// your code here
}
}
Now Spring is able to recognize CreateProjectService as a concrete implementation, instantiates it as a Spring bean, keeps it in Spring container and make it available for Dependency Injection wherever injected.
Hope this helps out.
UPDATE:
It is more advisable to inject dependencies via constructor than field or setter method.
#Restcontroller
#RequestMapping("/api/v1")
public class CreateProjectRest {
private ICreateProjectService iCreateProjectService;
/* No needed to annotate #Autowired after Spring 4.3.*/
public CreateProjectRest(ICreateProjectService iCreateProjectService){
this.iCreateProjectService = iCreateProjectService;
}
#PostMapping
public Projects execute(Projects project) {
createProjectService.execute(project);
// your code here
}
}
Until Spring Boot 2.0, I could reproduce the controllers generated for query methods exposed by a mongodb repository.
Here is a code sample:
Domain Entity
#Document(collection = "foos")
public class Foo {
#Id
private String id;
private String name;
// getters/setters omitted
}
Mongo repository
public interface FooRepository extends MongoRepository<Foo, String> {
public Page<Foo> findByName(#Param("name") String name, Pageable pageable);
}
Spring Boot automatically exposes the search method through /foos/search/findByName?name=... with a result similar to this:
{
"_embedded" : {
"foos" : [ {
"name" : "qc",
"_links" : {
"self" : {
"href" : "http://localhost:8080/foos/56a8a8d5daffd28c9c907974"
},
"foo" : {
"href" : "http://localhost:8080/foos/56a8a8d5daffd28c9c907974"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/foos/search/findByName?name=qc&page=0&size=20"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
I could reproduce it with the following custom controller and configuration
#RestController
#RequestMapping("foos")
#RequiredArgsConstructor // lombok
public class FooQueryController {
private final FooRepository repository;
private final PagedResourcesAssembler pagedResourcesAssembler;
#GetMapping(value = "search/query",
produces = MediaType.APPLICATION_JSON_UT8_VALUE)
public ResponseEntity custom(#RequestParam("name") String name,
Pageable pageable,
PersistentEntityResourceAssembler resourceAssembler) {
var page = repository.findByName(name, pageable);
var model = pagedResourcesAssembler.toResource(page, resourceAssembler);
return ResponseEntity.ok(model);
}
}
// Enables injecting a PersistentEntityResourceAssembler in a RestController
// see https://jira.spring.io/browse/DATAREST-657 for details
#Configuration
#Order(Ordered.HIGHEST_PRECEDENCE)
#RequiredArgsConstructor
public class MvcConfiguration implements WebMvcConfigurer {
// WARNING: do NOT change the name of this member - it is injected with the
// RequestMappingHandlerAdapter$repositoryExporterHandlerAdapter().
private final RequestMappingHandlerAdapter repositoryExporterHandlerAdapter;
#Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
List<HandlerMethodArgumentResolver> customArgumentResolvers =
repositoryExporterHandlerAdapter.getCustomArgumentResolvers();
argumentResolvers.addAll(customArgumentResolvers);
}
}
With this I can send a request to /foos/search/query?name=... and get the expected response:
{
"_embedded" : {
"foos" : [ {
"name" : "qc",
"_links" : {
"self" : {
"href" : "http://localhost:8080/foos/56a8a8d5daffd28c9c907974"
},
"foo" : {
"href" : "http://localhost:8080/foos/56a8a8d5daffd28c9c907974"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/foos/search/query?name=qc&page=0&size=20"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
Switching to Spring Boot 2.3, using the Spring HATEOAS 1.0 API in the controller
#RestController
#RequestMapping("foos")
#RequiredArgsConstructor
public class FooQueryController {
private final FooRepository repository;
private final PagedResourcesAssembler pagedResourcesAssembler;
#GetMapping(value = "search/query",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity custom(#RequestParam("name") String name,
Pageable pageable,
PersistentEntityResourceAssembler resourceAssembler) {
var page = repository.findByName(name, pageable);
var model = pagedResourcesAssembler.toModel(page, resourceAssembler);
return ResponseEntity.ok(model);
}
}
I now get the following result:
{"_embedded":{"foos":[{"id":"56a8a8d5daffd28c9c907974","name":"qc","embeddeds":{},"nested":false,"persistentEntity":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":{"name":"id","rawType":"java.lang.String","association":false,"owner":{"idProperty":
With the following errors in Spring's log:
2020-08-06 18:11:20.968 WARN 9932 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:472) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.sendServerError(DefaultHandlerExceptionResolver.java:550) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:440) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:210) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:141) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1300) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1111) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
2020-08-06 18:11:20.979 ERROR 9932 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"]->org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"]->org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"]->org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"]->org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"]->org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"]->org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"]->org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"]->org.springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"]->org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"]-> ... IT GOES ON AND ON LIKE THIS ... ->org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"])] with root cause
java.lang.StackOverflowError: null
at java.base/java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016) ~[na:na]
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:773) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178) ~[jackson-databind-2.11.1.jar:2.11.1]
... IT GOES ON LIKE THIS FOR DOZENS AND DOZENS OF LINE
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.11.1.jar:2.11.1]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:755) ~[jackson-databind-2.11.1.jar:2.11.1]
2020-08-06 18:11:21.553 ERROR 9932 --- [nio-8080-exec-1] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/foos/search/query] and exception [] as the response has already been committed. As a result, the response may have the wrong status code.
If if return the contents of var page = repository.findByName(name, pageable); instead of var model = pagedResourcesAssembler.toModel(page, resourceAssembler);, I get the following result:
{"content":[{"id":"56a8a8d5daffd28c9c907974","name":"qc"}],"pageable":{"sort":{"sorted":false,"unsorted":true,"empty":true},"offset":0,"pageNumber":0,"pageSize":20,"paged":true,"unpaged":false},"last":true,"totalPages":1,"totalElements":1,"size":20,"number":0,"sort":{"sorted":false,"unsorted":true,"empty":true},"numberOfElements":1,"first":true,"empty":false}
So the circular mess comes from the serialization pagedResourcesAssembler.toModel(page, resourceAssembler) (and probably the WebMvcConfigurer override).
I had the same StackOverflowError: null when I call PersistentEntityResourceAssembler.toModel(Object). My code returns a single EntityModel instead of a PagedModel. My problem is solved by switching to PersistentEntityResourceAssembler.toFullResource(Object).
The toModel(Object) uses excerpt projection while toFullResource(Object) does not. I don't have time to dig into the code to check what produces an infinite loop to cause stack overflow.
Look at the source code of PagedResourcesAssembler, PagedResourcesAssembler.toModel(Page, RepresentationModelAssembler) calls RepresentationModelAssember.toModel(Object).
So workaround 1 is copying the source code of PagedResourcesAssembler.toModel(Page, RepresentationModelAssembler) and use RepresentationModelAssember.toFullResource(Object).
Workaround 2 is extending the PagedResourcesAssembler and overriding the createModel(...) method.
Before making the workaround, I have one question. Why do you pass a PersistentEntityResourceAssembler to PagedResourcesAssembler? My code uses PersistentEntityResourceAssembler to add HTTP headers ETag and Last-Modified when my custom controller returns a single resource. What good does it have to use PersistentEntityResourceAssembler to build the response of a collection resource (paged resource)?
Thanks to this thread on Spring JIRA, I was able to:
convert the controller MultiValueMap parameters to a Predicate
use a RepositoryRestController instead of a RestController, ie be able to inject a PersistentEntityResourceAssembler into the controller method.
Custom models and models assembler, as suggested in the accepted answer, are not necessary.
Configure a QuerydslPredicateBuilder bean:
#Configuration
#RequiredArgsConstructor
public class QueryDslConfiguration {
private final ConversionService mvcConversionService;
private final QuerydslBindingsFactory querydslBindingsFactory;
#Bean
public QuerydslPredicateBuilder querydslPredicateBuilder() {
return new QuerydslPredicateBuilder(mvcConversionService, querydslBindingsFactory.getEntityPathResolver());
}
}
A service to convert a MultiValueMap to a Predicate:
#Service
#RequiredArgsConstructor(onConstructor = #__(#Autowired))
public class PredicateService {
private final QuerydslPredicateBuilder querydslPredicateBuilder;
private final QuerydslBindingsFactory querydslBindingsFactory;
public <T> Predicate getPredicateFromParameters(final MultiValueMap<String, String> parameters, Class<T> tClass) {
TypeInformation<T> typeInformation = ClassTypeInformation.from(tClass);
return querydslPredicateBuilder.getPredicate(typeInformation,
parameters,
querydslBindingsFactory.createBindingsFor(typeInformation));
}
}
Use the converter in a controller:
#RepositoryRestController
#RequiredArgsConstructor
public class FooController {
private final FooRepository repository;
private final PredicateService predicateService;
#GetMapping("/foos/search/query")
public PagedModel<Foo> query(
#RequestParam MultiValueMap<String, String> parameters,
Pageable pageable,
PersistentEntityResourceAssembler resourceAssembler) {
Predicate predicate = predicateService.getPredicateFromParameters(parameters, Foo.class);
Page<Parameter> page = repository.findAll(predicate,
return pagedResourcesAssembler.toModel(page, resourceAssembler);
}
}
I am testing an API which is supposed to fetch a document from an envelope. Here is the example code:
#RestController
#Controller
#RequestMapping(value = "DocuSign")
public class DocusignController extends CoreController {
#RequestMapping(value = "/FetchPDF", method = RequestMethod.GET)
#ResponseBody
public MessageItem downloadDocument(Model model) throws ApiException, IOException {
WorkArguments args = new WorkArguments();
args.setAccountId("1e8dce91-c5....");
args.setDocumentId("1");
args.setEnvelopeId("ec1f77....");
String accessToken = "eyJ0eXAiOiJNVCIsImFsZyI6I.....";
String baseUrl = "https://demo.docusign.net/restapi";
JSONObject obj = doWork(args, null, accessToken, baseUrl);
MessageItem msg = new MessageItem();
msg.setMsg("Done!");
return msg;
}
protected JSONObject doWork(WorkArguments args, ModelMap model,
String accessToken, String basePath) throws ApiException, IOException {
// Data for this method
// accessToken (argument)
// basePath (argument)
String accountId = args.getAccountId();
String envelopeId = args.getEnvelopeId();
String documentId = args.getDocumentId();
ApiClient apiClient = new ApiClient(basePath);
apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
EnvelopesApi envelopesApi = new EnvelopesApi(apiClient);
System.out.println("--->Checking if anything is null...");
if(envelopesApi == null )
System.out.println("Enveloper API is null!");
if(apiClient == null)
System.out.println("The apiClient is null!");
// Step 1. EnvelopeDocuments::get.
// Exceptions will be caught by the calling function
byte[] results = envelopesApi.getDocument(accountId, envelopeId, documentId);
String mimetype = ".pdf";
String docName = "success";
... etc.
When I go and test it with my rest client, I keep getting the following error:
Type Exception Report
Message Request processing failed; nested exception is
com.sun.jersey.api.client.ClientHandlerException
Description The server encountered an unexpected condition that
prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
com.sun.jersey.api.client.ClientHandlerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
com.sun.jersey.api.client.ClientHandlerException
com.docusign.esign.client.auth.OAuth.updateAccessToken(OAuth.java:111)
com.docusign.esign.client.auth.OAuth.applyToParams(OAuth.java:99)
com.docusign.esign.client.ApiClient.updateParamsForAuth(ApiClient.java:1209)
com.docusign.esign.client.ApiClient.getAPIResponse(ApiClient.java:1094)
com.docusign.esign.client.ApiClient.invokeAPI(ApiClient.java:1158)
com.docusign.esign.api.EnvelopesApi.getDocument(EnvelopesApi.java:2624)
com.docusign.esign.api.EnvelopesApi.getDocument(EnvelopesApi.java:2556)
delaware.gov.dti.ice.esignprototypev1.controller.DocusignController.doWork(DocusignController.java:82)
delaware.gov.dti.ice.esignprototypev1.controller.DocusignController.downloadDocument(DocusignController.java:52)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NullPointerException
com.docusign.esign.client.auth.OAuth.updateAccessToken(OAuth.java:109)
com.docusign.esign.client.auth.OAuth.applyToParams(OAuth.java:99)
com.docusign.esign.client.ApiClient.updateParamsForAuth(ApiClient.java:1209)
com.docusign.esign.client.ApiClient.getAPIResponse(ApiClient.java:1094)
com.docusign.esign.client.ApiClient.invokeAPI(ApiClient.java:1158)
com.docusign.esign.api.EnvelopesApi.getDocument(EnvelopesApi.java:2624)
com.docusign.esign.api.EnvelopesApi.getDocument(EnvelopesApi.java:2556)
delaware.gov.dti.ice.esignprototypev1.controller.DocusignController.doWork(DocusignController.java:82)
delaware.gov.dti.ice.esignprototypev1.controller.DocusignController.downloadDocument(DocusignController.java:52)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
I am using docusign-esign-java version 3.2.0, which is cucrently the latest. I am able to fetch the doucment when using the rest client, so I know that at least my account id, access token and document id (and template id) are correct. Also I am basing my solution from the following DocuSign refrence page Downloading Envelope Documents and using the provided downloaded code just for to doubleche to see if I am missing anything.
Furthermore I checked to see if the apiClient or the envelopesApi objects are null and they are not so I am getting the problem on the
byte[] results = envelopesApi.getDocument(accountId, envelopeId, documentId);
which for some reason, seems to give a null pointer exception. Any idea as to why?
I updated existing example without Spring to test 3.2.0 version, and it seems to work fine for me and it is not throwing any exception. If it is working w/o Spring then it will work with Spring as well, so does not look like any issue in SDK.
public void listDocuments(String envelopeId) throws ApiException, IOException {
this.checkToken();
EnvelopesApi envelopeApi = new EnvelopesApi(this.apiClient);
EnvelopeDocumentsResult envelopeDocumentsResult = envelopeApi.listDocuments(this.getAccountId(), envelopeId);
List<EnvelopeDocument> envelopeDocuments = envelopeDocumentsResult.getEnvelopeDocuments();
for (EnvelopeDocument envelopeDocument : envelopeDocuments) {
System.out.println("documentId " + envelopeDocument.getDocumentId());
envelopeApi.getDocument(this.getAccountId(), envelopeId, envelopeDocument.getDocumentId());
}
}