Spring Data REST returns http 405 asking for List of nested resources

喜夏-厌秋 提交于 2019-12-07 19:59:26

问题


I'm using Spring Boot 1.5.7, Spring Data REST, Spring HATEOAS, Hibernate, Spring Validation, Swagger.

I'm exposing all my repositories via Spring Data REST. It works quite fine but I've a problem when I expose a nested list of objects.

Let's see this example:

@Entity
public class TicketBundle extends AbstractEntity {

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY,mappedBy="ticketBundle")
    @OnDelete(action = OnDeleteAction.NO_ACTION)
    private List<MovementTicketBundle> payments = new ArrayList<>();

this is my Movement superclass

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public class Movement extends AbstractEntity {
    @Column(nullable = false)
    protected String description;

and this the specific class used from TicketBundle:

@Entity
@DiscriminatorValue(value = "ticketBundle")
public class MovementTicketBundle extends Movement {
    private static final long serialVersionUID = 3949580014012377816L;

    @ManyToOne(fetch = FetchType.LAZY)
    TicketBundle ticketBundle;

I've one repository for each bean:

@Transactional
@PreAuthorize("isAuthenticated()")
public interface MovementRepository extends PagingAndSortingRepository<Movement, Long> {
}

and @Transactional @PreAuthorize("isAuthenticated()") public interface MovementTicketBundleRepository extends PagingAndSortingRepository { }

and

@Transactional
@PreAuthorize("isAuthenticated()")
public interface TicketBundleRepository extends PagingAndSortingRepository<TicketBundle, Long> {
}

In Swagger I see my TicketBundle:

I'm trying to use the endpoint GET http://localhost:8080/api/v1/ticketBundles/1/payments that should return a list of MovementTicketBundle. This is instead what happens:

curl -X GET --header 'Accept: application/hal+json' 'http://localhost:8080/api/v1/ticketBundles/1/payments'

response code: 405 response body: no content response headers:

{
  "allow": "POST",
  "content-length": "0",
  "date": "Fri, 13 Oct 2017 07:52:11 GMT",
  "x-application-context": "application:prod",
  "x-content-type-options": "nosniff",
  "x-frame-options": "DENY",
  "x-xss-protection": "1; mode=block",
  "content-type": null
}

If the nested resourse is a bean everything works. The problem is when I've list. Am I missing something? Do you have some advice to solve the problem? I didn't see other questions about this problem.

来源:https://stackoverflow.com/questions/46725154/spring-data-rest-returns-http-405-asking-for-list-of-nested-resources

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