Spring Data REST端点消失(休眠冲突)

时间:2018-10-17 23:49:21

标签: java spring spring-mvc spring-boot spring-data-rest

我正在尝试将项目更新为Spring Boot 2.0.5版本。这是 简单的github project

基本部分:

@Entity
@Table(name = "placement")
public class Placement {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(updatable = false)
    private Long id;

    private String name;

    @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
    private PlacementType type;

    //... getters and setters
}

@Entity
@Table(name = "placement_type")
public class PlacementType  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(updatable = false)
    private Long id;

    private String name;
    //... getters and setters
}

@RepositoryRestResource(collectionResourceRel = "placement", path = "placement")
public interface PlacementRepository extends 
     PagingAndSortingRepository<Placement, Long> {}

@RepositoryRestResource(collectionResourceRel = "placementType", path = "placementType")
public interface PlacementTypeRepository extends 
     PagingAndSortingRepository<PlacementType, Long> {}

build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.5.RELEASE'
    }

...

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-data-rest:${springBootVersion}")
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.3.6.Final'
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.9.0'
    compile 'com.h2database:h2:1.4.196'
}

问题:

当我启动应用程序时,两个api端点都可以正常工作:

GET http://localhost:8090/api/placement/ => 200 works fine
GET http://localhost:8090/api/placementType => 200 works fine

但是在我从Placement uri(@ManyToOne)访问PlacementType之后,我无法再访问PlacementType端点:

GET http://localhost:8090/api/placement/1/type => 200 works fine
GET http://localhost:8090/api/placementType => 404 (and default spring error screen)

没有错误日志,或者我的日志级别不正确,但是我不知道为什么第二次将请求重定向到SimpleUrlHandlerMapping。

 LOG from first GET http://localhost:8090/api/placementType => 200 
 02:38:56.076 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/api/placementType] 
 02:38:56.080 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /api/placementType 
 02:38:56.088 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/api/placementType] 
 02:38:56.114 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Last-Modified value for [/api/placementType] is: -1 
 Hibernate: select placementt0_.id as id1_1_, placementt0_.name as name2_1_ from placement_type placementt0_ limit ?
 02:38:56.559 [http-ni] DEBUG m.m.a.RequestResponseBodyMethodProcessor - Written [PagedResource { content: [Resource { content: com.varren.model.PlacementType@65e962bd, links: [<http://localhost:8090/api/placementType/1>;rel="self", <http://localhost:8090/api/placementType/1>;rel="placementType"] }], metadata: Metadata { number: 0, total pages: 1, total elements: 1, size: 20 }, links: [<http://localhost:8090/api/placementType{?page,size,sort}>;rel="self", <http://localhost:8090/api/profile/placementType>;rel="profile"] }] as "application/hal+json" using [org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$ResourceSupportHttpMessageConverter@446c8c72] 
 02:38:56.562 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
 02:38:56.563 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Successfully completed request 

                 GET http://localhost:8090/api/placement/1/type => 200 works fine
 LOG from second GET http://localhost:8090/api/placementType => 404 
 03:09:08.285 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/api/placementType] 
 03:09:08.286 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /api/placementType 
 03:09:08.287 [http-ni] DEBUG s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/api/placementType] 
 03:09:08.292 [http-ni] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping        - Matching patterns for request [/api/placementType] are [/**] 
 03:09:08.292 [http-ni] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping        - URI Template variables for request [/api/placementType] are {} 
 03:09:08.292 [http-ni] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping        - Mapping [/api/placementType] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@74eec640]]] and 1 interceptor 
 03:09:08.293 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Last-Modified value for [/api/placementType] is: -1 
 03:09:08.294 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
 03:09:08.294 [http-ni] DEBUG o.s.w.s.DispatcherServlet                - Successfully completed request 

还有一件事。大多数时候我都有这个错误,但是有时候一切都很好。我在github存储库中添加了第二个@Entity,以表明有时2个相同的实体之一有效而另一个不起作用,但也许我缺少明显的东西

1 个答案:

答案 0 :(得分:1)

问题似乎出在hibernate-entitymanager中,或者至少是版本不匹配

compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.3.6.Final'

花了我几天时间才意识到不再需要它,我应该切换到hibernate-core。但是切换到最新的休眠内核也会产生相同的问题,因此我不得不完全删除依赖项,并让spring-boot-starter-data-jpa使用它的版本(5.2.17.Final用于引导2.0.5.RELEASE)。

使用RepositoryRestHandlerMapping.lookupHandlerMethod中的断点对其进行调试,并在RepositoryRestHandlerMapping缓存中注意到奇怪的class com.varren.model.PlacementType$HibernateProxy

enter image description here

正常(工作)版本看起来像这样(没有HibernateProxy):https://i.stack.imgur.com/1V0FU.png