gpt4 book ai didi

spring-boot - 如何将 "book"添加到/shelves/1/books

转载 作者:行者123 更新时间:2023-12-04 03:16:36 30 4
gpt4 key购买 nike

我不知道如何做一些应该非常简单的事情。

我有两个实体:书架和书。一个书架可以放一本或多本书。这些实体中的每一个都有一个相应的 JpaRepository 暴露为使用 Spring Data Rest 的 rest 存储库。当我运行该应用程序时,所有查询端点都运行良好,但我不知道如何将一本书添加到书架。

首先,我通过将 { "name":"westerns"} 发布到/shelves 来添加一个架子(效果很好)。

方法 1: 我尝试通过将 { "name":"mybook"} 发布到/shelves/1/books 来添加一本书,但我收到 405“不允许的方法”错误。我能否只将一本书发布到/books 端点(我的意思是不编写我自己的 Controller )?如果是这样,我是否必须嵌入这本书所属的书架实体?

方法 2: 如果我尝试使用内容 { "name":"westerns", "books":[ {"name"通过 PUT 到/shelves/1 来添加一本书:"mybook"} ] },我收到以下错误消息:

消息:“无法读取 JSON:模板不能为 null 或为空!(通过引用链:org.demo.Shelf["books"]);嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:模板不能为 null 或空!(通过引用链:org.demo.Shelf["books"])

统计跟踪:

2014-11-06 06:55:18.864 ERROR 9888 --- [nio-8080-exec-5] s.d.r.w.AbstractRepositoryRestController : Could not read JSON: Template must not be null or empty! (through reference chai n: org.demo.Shelf["books"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Template must not be null or empty! (through reference chain: org.demo.Shelf[" books"])

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Template must not be null or empty! (through reference chain: org.demo.Shelf["books"]); nes ted exception is com.fasterxml.jackson.databind.JsonMappingException: Template must not be null or empty! (through reference chain: org.demo.Shelf["books"]) at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228) at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:212) at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:159) at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:100 ) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:874) at javax.servlet.http.HttpServlet.service(HttpServlet.java:649) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1695) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: com.fasterxml.jackson.databind.JsonMappingException: Template must not be null or empty! (through reference chain: org.demo.Shelf["books"]) at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232) at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1420) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:244) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158) at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225) ... 38 common frames omitted Caused by: java.lang.IllegalArgumentException: Template must not be null or empty! at org.springframework.util.Assert.hasText(Assert.java:162) at org.springframework.hateoas.UriTemplate.(UriTemplate.java:56) at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$UriStringDeserializer.deserialize(PersistentEntityJackson2Module.java:380) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242) ... 42 common frames omitted

这是所有相关代码(当然,每个类都在自己的文件中)。

@Entity
public class Shelf {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String name;

@OneToMany
private List<Book> books;

public List<Book> getBooks() {
return books;
}

public void setBooks(List<Book> books) {
this.books = books;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

@Entity
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}

@RepositoryRestResource
public interface BookRepository extends JpaRepository<Book, Long>{

}

@RepositoryRestResource
public interface ShelfRepository extends JpaRepository<Shelf, Long>{

}

我正在使用 Spring Boot 1.1.8.RELEASE

最佳答案

Book 实体有自己的 REST 端点,要创建一个您必须 POST 到该端点。

要将一本书放在书架上,您必须将该书的 URI 发送到关联 URI (/shelves/{id}/books)。令人惊讶的是,文档有误,添加项目不是通过 POST 而是通过 PATCH 完成的:

PATCH http://localhost:8080/shelves/1/books
Content-Type: text/uri-list
http://localhost:8080//books/1

您可以一次添加多本书,每行一个 URI。如果您不想添加图书,而是将书架上的所有图书替换为新图书,即替换整个馆藏,您可以使用 PUT 代替。

关于spring-boot - 如何将 "book"添加到/shelves/1/books,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26768897/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com