2017-10-30 8 views
0

多対多リレーションシップでクエリを実行しようとしています。その結果、私にとっては意味のない大きなJsonと、春のコンソールではエラーが発生します。私はなぜそれが起こるのか分からない。私は、多くの関係に多くを行うためのチュートリアルにより、ガイドgetOutputStream()はすでにスプリングブートと呼ばれています

だった。これは、これはこれは

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name="PRODUCTO_ID") 
private long productoId; 

private String name; 

private Long price; 


@ManyToMany(mappedBy = "products") 
private Set<Customer> customers = new HashSet<>(); 

このカスタマー

@Id 
@GeneratedValue 
private Long customerId; 
private String name; 
private String email; 

@ManyToMany(cascade = {CascadeType.ALL}) 
@JoinTable(
     name= "PRODUCTS_BY_CUSTOMER", 
     joinColumns = { @JoinColumn(name = "CUSTOMER_CUSTOMER_ID")}, 
     inverseJoinColumns = { @JoinColumn(name = "PRODUCT_PRODUCTO_ID")} 
     ) 
Set<Product> products = new HashSet<>(); 

製品であるリポジトリ

public interface ProductJpaRepository extends JpaRepository<Customer, Long> { 

@Query(value = "select p from Product p where p.productoId = ?1") 
    Product findByProductId(Long productId); 

List<Customer> findBycustomerId(Long customerId); 


} 

ですコントローラ

@GetMapping(value = "productsList/{productId}") 
public Product findByProductId(@PathVariable final Long productId){ 
    return productJpaRepository.findByProductId(productId); 
} 

これはブラウザhttp://localhost:8180/products/productsList/3に入れたときの結果です。 この結果は、私は一部のみを添付し、はるかに延びるが、残りは同じ

{ "productoId" である:3、 "名前": "Producto C"、 "価格":4000、 "顧客": [{"customerId":1、 "name": "Carlos Rodriguez"、 "email": "[email protected]"、 "products":[{"productoId":3、 "name": "Producto C" "price":4000、 "customers":[{"customerId":1、 "name": "Carlos Rodriguez"、 "email": "[email protected]"、 "products":[{"productoId":3 "名前": "商品" C "、"価格 ":4000、"顧客 ":[{"顧客ID ":1、"名前 ":"カルロスロドリゲス "、"電子メール ":" [email protected] "製品 ":

そして最後に、これは、コンソールにエラーがある

com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeで

(CollectionSerializer.java:25)〜[ジャクソン-データバインド-2.8.10.jar:2.8.10] com.fasterxmlで 。 jsonson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704)〜[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields( BeanSerializerBase.java:689)〜[jackson-databind-2.8.10.jar:2.8.10] (com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)〜[jackson-databind- 2.8.10.jar:2.8.10] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)〜[jackson-databind-2.8.10.jar:2.8.10] at c om.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)〜[jackson-databind-2.8.10.jar:2.8.10] (com.fasterxml.jackson.databind.ser)。 std.CollectionSerializer.serialize(CollectionSerializer.java:25)〜[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) 〜[jackson-databind-2.8.10.jar:2.8.10] (com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689)〜[jackson-databind-2.8.10)。 jar:2.8.10] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)〜[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml。 jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)〜[jackson-databind-2.8.10.jar:2.8 .10] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)〜[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml.jackson .databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)〜[jackson-databind-2.8.10.jar:2.8.10] (com.fasterxml.jackson.databind.ser.BeanPropertyWriter)。com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689)で 〜[:serializeAsField(BeanPropertyWriter.java:704)〜[2.8.10ジャクソン-データバインド-2.8.10.jar] jackson-databind-2.8.10.jar:2.8.10] (com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)〜[jackson-databind-2.8.10.jar:2.8。 10] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)〜[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml.jackson。 databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)〜[ジャクソン-データバインド-2.8.10.jar:2.8.10]

2017年10月29日20:17:05.427 ERROR 1640 --- [サーブレット[dispatcherServlet]のServlet.service()が例外をスローしました

java.lang.IllegalStateException:getOutputStream()がすでに呼び出されていますorg.apache.catalina.connector.Response.getWriter(Response.java:625)でこのレスポンス ため〜[Tomcatの-埋め込みコア8.5.23.jar:8.5.23] org.apache.catalina.connectorで.ResponseFacade.getWriter(ResponseFacade.java:211)〜[Tomcatの-埋め込みコア8.5.23.jar:8.5.23] javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:109)〜で [Tomcatの、埋め込み-core-8.5.23.jar:org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration $ SpelView.render(ErrorMvcAutoConfiguration.java:227)〜[春-ブート自動構成-1.5.8.RELEASEで8.5.23] .jar:1.5.8。 RELEASE] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1286)〜[spring-webmvc-4.3.12.RELEASE]: at org.springframework.web。 servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1041)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java: 984)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)〜[spring-webmvc-4.3。 12.RELEASE.jar:4.3.12.RELEASE] 、org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12。 RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkS (HttpServlet.java:635)〜[tomcat-embed-service](ja-jp):[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] :javax.servlet.http.HttpServlet.service core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12。 RELEASE] (javax.servlet.http.HttpServlet.service(HttpServlet.java:742)〜〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain)。 internalDoFilter(ApplicationFilterChain.java:231)〜[Tomcatの-埋め込みコア8.5.23.jar:8.5.23] org.apache.catalina.core.ApplicationFilterChain.doFilterで (ApplicationFilterChain.java:166)〜[tomcat- embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)〜[spring-web-4.3 .12.RELEASE.jar:org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)〜[春-ウェブ4.3.12.RELEASE.jarで4.3.12.RELEASE] :4.3.12 .RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core .ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)〜 [spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE] をorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[Tomcat-embed-core-8.5.23 .jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain。doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)〜[spring- web-4.3.12.RELEASE.jar:4.3.12.RELEASE] をorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[Tomcat-embed-core-8.5.23.jar: 8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web。 filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java: 193)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina。 core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728) [tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:590)[tomcat-embed-core-8.5.23.jar: 8.5.23] at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:524)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core .StandardHostValve.custom(StandardHostValve.java:389)[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:254)[tomcat -embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardHostValve.throwable(StandardHostVa (Tomcat-embed-core-8.5.23.jar:8.5.23) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:175)[tomcat-embed-core- 8.5.23.jar:8.5.23] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)[tomcat-embed-core-8.5.23.jar:8.5.23] at org .apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter。 java:342)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)[tomcat-embed-core-8.5。 23.jar:8.5.23] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor .doRun(NioEndpoint.java:1459)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)[tomcat -embed-core-8.5.23.jar:8.5.23] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[na:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)[na:1.8.0_131] (org.apache.tomcat.util.threads.TaskThread)$ WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-8.5。 23.jar:8.5.23] at java.lang.Thread.run(Thread.java:748)[na:1.8.0_131]

2017-10-29 20:17:05.458エラー1640 --- [nio-8180-exec-1] oaccC [Tomcat]。[localhost]:例外処理ErrorPage [errorCode = 0、location =/error]

org.springframework.web.util.NestedServletException:要求の処理に失敗しました。ネストされた例外はjava.lang.IllegalStateExceptionです。この応答に対してgetOutputStream()がすでに呼び出されています at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)〜[spring-webmvc-4.3.12.RELEASE .jar:4.3.12.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] javax.servlet.http.HttpServlet.service(HttpServlet。java:635)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)〜[spring-webmvc-4.3。 12.RELEASE.jar:4.3.12.RELEASE] 、javax.servlet.http.HttpServlet.service(HttpServlet.java:742)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter( ApplicationFilterChain.java:166)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)〜[spring-web- 4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java: 107)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core- 8.5.23.jar:8.5.23] 、org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.apache.catalina.core.ApplicationFilterChain。 internalDoFilter(ApplicationFilterChain.java:193)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[tomcat- embed-core-8.5.23.jar:8.5.23] at org.springframework.web.filter.OncePerRequestFilter.doFilte (ApplicationFilterChain.java:193)〜[spring- Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)〜[Tomcat-embed-core-8.5.23.jar: 8.5.23] 、org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)〜[spring-web-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.apache。 (ApplicationFilterChain.java:193)〜[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java: 166)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.Applic ationDispatcher.invoke(ApplicationDispatcher.java:728)〜[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:590)〜[ (Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:524)〜[Tomcat-embed-core-8.5.23.jar: 8.5.23] at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:389)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core .StandardHostValve.status(StandardHostValve.java:249)[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:349)[tomcat -embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:175)[tomcat -embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)[tomcat-embed-core-8.5.23.jar:8.5。 23] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.connector.CoyoteAdapter .service(CoyoteAdapter.java:342)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)[tomcat-embed -core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:868)[tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache。Tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase.java:49)[tomcat-embed-core-8.5.23.jar:8.5.23] のjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[na:1.8.0_131] java.util.concurrent.ThreadPoolExecutorで$ Worker.run(ThreadPoolExecutor.java:617)[na:1.8.0_131] at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23] java.lang.Thread.run(Thread.java:748)[na:1.8.0_131] 原因:java.lang.IllegalStateException:この応答に対してgetOutputStream()がすでに呼び出されています at org.apache.catalina.connector.Response.getWriter(Responsible (ResponseFacade.java:211)〜[Tomcat-embed-core-8.5.23.jar:8.5.23] at org.apache.catalina.connector.ResponseFacade.getWriter〜 core-8.5.23.jar:8.5.23] at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:109)〜[Tomcat-embed-core-8.5.23.jar:8.5.23] at org。 springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration $ SpelView.render(ErrorMvcAutoConfiguration.java:227)〜[spring-boot-autoconfigure-1.5.8.RELEASE.jar:1.5.8.RELEASE] at org.springframework.web。 servlet.DispatcherServlet.render(DispatcherServlet.java:1286)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java: 1041)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web .servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:984)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java :901)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)〜[spring-webmvc-4.3 .12.RELEASE.jar:4.3.12.RELEASE] ... 38の共通フレームは、あなたがあなたのProductエンティティおよびその逆にCustomerの組成を有し

答えて

2

を省略しました。

エンティティProductJSONに変換している途中で終了する、終了しないシリアル化のために、このエラーが発生しています。

Productエンティティにcustomersのいずれか

  • JsonIgnoreを試してみてください。

  • 完全なProductオブジェクトをシリアル化する代わりに、クライアント側で必要なフィールドのみを含むProductDtoのDTOクラスを作成します。

    @GetMapping(value = "productsList/{productId}") 
    public ProductDto findByProductId(@PathVariable final Long productId){ 
        Product product = productJpaRepository.findByProductId(productId); 
        ... 
        //get required properties from product and set in the ProductDto object 
        return productDto; 
    } 
    
+0

こんにちは@Abdullahカーン私はJsonIgnoreを入れなければならない理由、あなたは私を伝えることができますか?私がインターネットで見た例では、JsonIgnoreは言及されず、プログラムは正常に動作します。私はjasonIgnoreを試してみたところ、エラーは消えてしまった –

+0

前述のように、主な問題はオブジェクトに循環依存関係があるからです。 ProductエンティティでJsonIgnoreを使用すると、customersエンティティを含めなくてもエラーが消えます。それが受け入れられていれば答えをアップアップしてください。ありがとう –

関連する問題