After upgrade from Spring Boot 1.5 to 2.1 we get Target bean of type com.sun.proxy.$Proxy is not of type of the persistent entity

牧云@^-^@ 提交于 2021-01-29 06:54:21

问题


We upgraded from Spring Boot 1.5.13.RELEASE to 2.1.3.RELEASE and now a POST to Spring Data Rest with a projection is failing with this error:

java.lang.IllegalArgumentException: Target bean of type
 com.sun.proxy.$Proxy313 is not of type of the persistent entity
(com.*.*.*.BankAccount)!: com.sun.proxy.$Proxy313

This looks very similar to the old ticket: https://jira.spring.io/si/jira.issueviews:issue-html/DATAREST-1213/DATAREST-1213.html

The JUnit test passes:

  @Test
  public void shouldCreateEntity() throws Exception {
    ResultActions resultsActions = mockMvc.perform(post("/bankAccounts".concat("?projection=").concat(BankAccountDto.NAME))
        .content(new ObjectMapper().writeValueAsString(generateBankAccount())))
        .andExpect(status().isCreated())
        .andExpect(header().string("Location", containsString("bankAccounts/")))
        .andDo(verify().wiremock(
            WireMock.post(WireMock.urlPathMatching("/bankAccounts"))
        ).stub("create-bankAccount"))
        .andDo(document("create-bankAccount"));
  }

But, the POST to Spring Data Rest fails on the server.

    2019-03-28T11:13:13.401-05:00 [RTR/3] [OUT] account-service-qa-example.cfapps.io - [    2019-03-28T16:13:13.168+0000] 
    "POST /bankAccounts?projection=bankAccountDto HTTP/1.1" 500 205 185 "-" "Java/1.8.0_202" "00.00.0.00:57334" "00.00.000.00:61130" 
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at java.lang.Thread.run(Thread.java:748)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:679)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.cloudfoundry.router.ClientCertificateMapper.doFilter(ClientCertificateMapper.java:77)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at brave.servlet.TracingFilter.doFilter(TracingFilter.java:86)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.cloud.sleuth.instrument.web.ExceptionLoggingFilter.doFilter(ExceptionLoggingFilter.java:50)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at net.rakugakibox.spring.boot.logback.access.LogbackAccessSecurityAttributesSaveFilter.doFilter(LogbackAccessSecurityAttributesSaveFilter.java:28)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.filter.AbstractRequestLoggingFilter.doFilterInternal(AbstractRequestLoggingFilter.java:262)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at java.lang.reflect.Method.invoke(Method.java:498)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.rest.webmvc.RepositoryEntityController.postCollectionResource(RepositoryEntityController.java:272)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.rest.webmvc.RepositoryEntityController.createAndReturn(RepositoryEntityController.java:486)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.rest.webmvc.HttpHeadersPreparer.prepareHeaders(HttpHeadersPreparer.java:62)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at java.util.Optional.map(Optional.java:215)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.rest.webmvc.HttpHeadersPreparer.lambda$prepareHeaders$0(HttpHeadersPreparer.java:62)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.rest.webmvc.HttpHeadersPreparer.prepareHeaders(HttpHeadersPreparer.java:80)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.rest.webmvc.support.ETag.from(ETag.java:88)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.rest.webmvc.support.ETag.getVersionInformation(ETag.java:170)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.mapping.model.BasicPersistentEntity.getPropertyAccessor(BasicPersistentEntity.java:453)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.data.mapping.model.BasicPersistentEntity.verifyBeanType(BasicPersistentEntity.java:551)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.util.Assert.isInstanceOf(Assert.java:574)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] at org.springframework.util.Assert.instanceCheckFailed(Assert.java:655)
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT] java.lang.IllegalArgumentException: 
    Target bean of type com.sun.proxy.$Proxy313 is not of type of the persistent entity (com.*****.account.domain.BankAccount)!: com.sun.proxy.$Proxy313
    2019-03-28T11:16:39.058-05:00 [APP/PROC/WEB/0] [OUT]     2019-03-28 16:16:39.056 ERROR [account-service,1c845c447ea36328,7e8da14a044d1ab6,true] 
    [080-exec-6] o.s.d.r.w.RepositoryRestExceptionHandler:168 : 
    Target bean of type com.sun.proxy.$Proxy313 is not of type of the persistent entity (com.*****.account.domain.BankAccount)!: com.sun.proxy.$Proxy313
    2019-03-28T11:16:39.065-05:00 [RTR/6] [OUT] account-service-qa-example.cfapps.io - [    2019-03-28T16:16:38.856+0000] 
    "POST /bankAccounts?projection=bankAccountDto HTTP/1.1" 500 205 185 "-" "Java/1.8.0_202" "10.10.66.83:32538" "00.00.000.00:61130" 

The call to Spring Data Rest is straightforward and has not changed.

  public Optional<String> createBankAccount(CreateBankAccountDto createBankAccountDto) {
    UriComponentsBuilder builder = UriComponentsBuilder
        .fromHttpUrl(BANK_ACCOUNTS)
        .queryParam(PROJECTION, BankAccountDto.PROJECTION);
    try {
      HttpEntity<CreateBankAccountDto> entity = new HttpEntity<>(createBankAccountDto);
      ResponseEntity<BankAccountDto> response = restTemplate
          .exchange(builder.build().encode().toUri(), HttpMethod.POST, entity,
              BankAccountDto.class);
      if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null
          && response.getBody().getId() != null) {
        return Optional.of(response.getBody().getId());
      }
    } catch (Exception e) {
      log.error(e.getMessage());
      log.debug(e.getMessage(), e);
    }
    return Optional.empty();
  }

Has anybody else experienced this problem after upgrading to Spring Boot 2.

来源:https://stackoverflow.com/questions/55404966/after-upgrade-from-spring-boot-1-5-to-2-1-we-get-target-bean-of-type-com-sun-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!