Spring Boot JPA - OneToMany relationship causes infinite loop

前端 未结 5 1317
走了就别回头了
走了就别回头了 2021-01-12 06:34

I have a two objects with simple @OneToMany relationship which looks as follows:

parent:

@Entity
public class ParentAccount {

  @Id
  @GeneratedValu         


        
5条回答
  •  甜味超标
    2021-01-12 06:50

    I recently had this issue due to a poorly defined Jackson2HttpMessageConverter.

    I had done something like the following.

    @Bean
    RestTemplate restTemplate(@Qualifier("halJacksonHttpMessageConverter")
                                      TypeConstrainedMappingJackson2HttpMessageConverter halConverter) {
        final RestTemplate template = new RestTemplateBuilder().build();
        halConverter.setSupportedMediaTypes(List.of(/* some media types */)); 
        final List> converters = template.getMessageConverters();
        converters.add(halConverter);
        template.setMessageConverters(converters);
        return template;
    }
    

    This caused a problem because the media types did not include all the defaults. Changing it to the following fixed the issue for me.

    halConverter.setSupportedMediaTypes(
        new ImmutableList.Builder()
            .addAll(halConverter.getSupportedMediaTypes())
            .add(/* my custom media type */)
            .build()
    );
    

提交回复
热议问题