Конечная точка REST Spring Data исчезает (спящий конфликт) - PullRequest
0 голосов
/ 18 октября 2018

Я пытаюсь обновить свой проект до версии весенней загрузки 2.0.5.А вот простой проект github

Необходимая часть:

@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

Но после того, как я захожу в PlacementType из URI места размещения (@ManyToOne), я не могу получить доступ к конечной точке 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 

Еще одинвещь.В большинстве случаев у меня есть эта ошибка, но иногда все работает просто отлично.Я добавил второй @Entity в репозиторий github, чтобы показать, что иногда одна из двух идентичных сущностей работает, а другая нет, но, возможно, я упускаю что-то очевидное

1 Ответ

0 голосов
/ 19 октября 2018

Похоже, проблема была в устаревшем hibernate-entitymanager или, по крайней мере, в несоответствии версий

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

Мне потребовалось несколько дней, чтобы понять, что он больше не нужен и что я должен переключиться на hibernate-core.Но переключение на последнее hibernate-core также вызвало ту же проблему, поэтому мне пришлось полностью удалить зависимость и позволить spring-boot-starter-data-jpa использовать ее версию (5.2.17.Final для загрузки 2.0.5.RELEASE).

Отладить это с помощьюточки останова в RepositoryRestHandlerMapping.lookupHandlerMethod и странное замечание class com.varren.model.PlacementType$HibernateProxy в кэше RepositoryRestHandlerMapping.

enter image description here

Обычная (рабочая) версия выглядит следующим образом (без HibernateProxy):https://i.stack.imgur.com/1V0FU.png

...