无法在Spring Boot中以OneToMany关系执行REST PUT操作

时间:2018-01-05 15:18:52

标签: spring spring-data-rest spring-restcontroller spring-repositories

以下是该方案:

我有两个实体

@Entity
@Data
class Project {

    @NotEmpty
    private String name;

    @OneToMany(cascade=ALL, orphanRemoval=true)
    @JoinColumn(name="project")
    private List<Module> modules = new ArrayList<>();
}

@Entity
class Module {

    @NotEmpty
    String content;

    @ManyToOne
    @JoinColumn(name="project")
    private Project project;
}

有三种类型的模块,ArticleFooterHeader

我创建了一个包含三个模块集合的Project

{
  "name" "Test project",
  "modules" : [
    { "type": "Header", "content": "Test title" },
    { "type": "Article", "content": "Bad news!" },
    { "type": "Footer", "content": "Test contact" }
  ]
}

我在POST/projects,感谢@RepositoryRestResource这一切都很有效。 Project - Test project现已在/projects/1下提供。

但现在我在PUT下使用数据

进行/projects/1
{
  "name" "Real test project",
  "modules" : [
    { "type": "Header", "content": "Better title" },
    { "type": "Article", "content": "Good news!" },
    { "type": "Article", "content": "Even better news!" },
    { "type": "Article", "content": "Wonderful news!" },
    { "type": "Footer", "content": "phone (00000)" }
  ]
}

它总是以

结束
org.springframework.http.converter.HttpMessageNotReadableException: Could not read an object of type class com.example.Project from the request!; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not read payload!; nested exception is java.lang.IllegalArgumentException: Target bean of type com.example.Article is not of type of the persistent entity (com.example.Footer)!
    at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.readPutForUpdate(PersistentEntityResourceHandlerMethodArgumentResolver.java:228) ~[spring-data-rest-webmvc-2.6.9.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:194) ~[spring-data-rest-webmvc-2.6.9.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:141) ~[spring-data-rest-webmvc-2.6.9.RELEASE.jar:na]
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:883)

修改

看起来问题已经被调查https://jira.spring.io/browse/DATAREST-1012

0 个答案:

没有答案