2017-11-04 7 views
0

私は2つのエンティティを持っています:ユーザーとアドレス。 次のように関係が設定されている:スプリングデータREST +休止状態5 +ジャクソンLAZYシリアル化失敗

ADDRESS

@Entity 
@Table(name = "addresses") 
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
public class Address { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @JsonIgnore 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "username") 
    private User user; 

USER

@Entity 
@Table(name = "users") 
public class User { 
    @Id 
    private String username; 

    (...) 

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) 
    private List<Address> addresses; 

私は1つのまたは複数のアドレスエンティティを持つユーザのユーザ名のためGET http://localhost:8080/api/data/users/{USERNAME}/addressesを呼び出すたびに、結果は:

言及する価値も

春データRESTは、リンク生成にもかかわらず、事実である:

"_links": { "self": { "href": "http://localhost:8080/api/data/addresses/33" }, "address": { "href": "http://localhost:8080/api/data/addresses/33" }, "user": [ { "href": "http://localhost:8080/api/data/users" }, { "href": "http://localhost:8080/api/data/addresses/33/user" } ] }

http://localhost:8080/api/data/addresses/33/userリンクがまったく機能していません。 (投げます。java.lang.NoClassDefFoundError:javaxの/サーブレット/ JSP/JSTL /コア/コンフィギュレーション)

これまでのところ、私はアドレスのエンティティにEAGER FetchingTypeにLAZYを変更し、次のように動作が、その後変更しようとした:

  • IできないGET http://localhost:8080/api/data/addresses(エラーメッセージ "Idがシリアライズ!: to.wysylam.couriersystem.api.entities.Userに割り当てなければならない" と500)

  • I缶GET http://localhost:8080/api/data/users/{USERNAME}/addresses

正直言って私は今考えていません。次のように

コンフィグファイルが定義されています。

@Configuration 
@ComponentScan(basePackages = { "to.wysylam.couriersystem.api.controllers", 
          "to.wysylam.couriersystem.api.services", 
          "to.wysylam.couriersystem.api.hateoas" 
          }) 
@Import({JpaConfig.class, 
    SecurityConfig.class, 
    DataRestConfig.class, 
    RepositoryRestMvcConfiguration.class 
}) 
public class AppConfig { 

} 

@Configuration 
public class DataRestConfig extends RepositoryRestConfigurerAdapter { 
    @Override 
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config){ 
    config.setRepositoryDetectionStrategy(
       RepositoryDetectionStrategy.RepositoryDetectionStrategies.ANNOTATED 
    ); 
    config.exposeIdsFor(User.class); 
    config.setBasePath("/data"); 
} 

@Bean 
protected Module module(){ 
    return new Hibernate5Module(); 
} 

@Override 
public void configureConversionService(ConfigurableConversionService configurableConversionService){ 
    configurableConversionService.addConverter(String.class, String[].class, stringToStringArrayConverter()); 
} 

private Converter<String, String[]> stringToStringArrayConverter(){ 
    return (source) -> StringUtils.delimitedListToStringArray(source, ";"); 

} 
} 

@Configuration 
@EnableJpaRepositories(basePackages = "to.wysylam.couriersystem.api.repositories") 
public class JpaConfig { 
private static Properties getJpaProperties(){ 
    Properties jpaProperties = new Properties(); 
    jpaProperties.put("hibernate.hbm2ddl.auto", "validate"); 
    jpaProperties.put("hibernate.default_schema", "couriersystem"); 
    jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect"); 
    jpaProperties.put("hibernate.enable_lazy_load_no_trans","true"); 
    return jpaProperties; 
} 

@Bean 
public static LocalContainerEntityManagerFactoryBean entityManagerFactory(){ 
    LocalContainerEntityManagerFactoryBean asBean = new LocalContainerEntityManagerFactoryBean(); 
    asBean.setDataSource(dataSource()); 
    asBean.setPackagesToScan("to.wysylam.couriersystem.api.entities"); 
    asBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 
    asBean.setJpaProperties(getJpaProperties()); 

    return asBean; 
} 

@Bean(name = "dataSource") 
public static DriverManagerDataSource dataSource(){ 
    DriverManagerDataSource bean = new DriverManagerDataSource(); 
    bean.setDriverClassName("org.postgresql.Driver"); 
    bean.setUrl("jdbc:postgresql://localhost:5432/postgres"); 
    bean.setUsername("dev"); 
    bean.setPassword("pwd"); 
    return bean; 
} 

@Bean 
public static JpaTransactionManager transactionManager(){ 
    JpaTransactionManager asBean = new JpaTransactionManager(); 
    asBean.setEntityManagerFactory(entityManagerFactory().getObject()); 
    return asBean; 
} 

@Bean 
public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){ 
    return new PersistenceExceptionTranslationPostProcessor(); 
} 

@Bean 
public static HibernateExceptionTranslator hibernateExceptionTranslator(){ 
    return new HibernateExceptionTranslator(); 
} 
} 

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "to.wysylam.couriersystem.api.controllers") 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@Import(AppConfig.class) 
public class WebConfig implements WebMvcConfigurer { 

@Bean 
public InternalResourceViewResolver viewResolver(){ 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setViewClass(JstlView.class); 
    viewResolver.setPrefix("/WEB-INF/pages/"); 
    viewResolver.setSuffix(".jsp"); 

    return viewResolver; 
} 

@Override 
public void addFormatters(FormatterRegistry registry){ 
    registry.removeConvertible(String.class, String[].class); 
    registry.addConverter(String.class, String[].class, stringToStringArrayConverter()); 
} 

private Converter<String, String[]> stringToStringArrayConverter(){ 
    return (source) -> StringUtils.delimitedListToStringArray(source, ";"); 
} 
} 

すべてのヘルプはこの間

UPDATE

を高く評価し、私は、文字列からロングにUserクラスのためのIDを変更しようとしました。

[DEBUG] 2017-11-06 18:25:01.053 [http-nio-8080-exec-39] ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.hateoas.Resources<?> org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.data.rest.webmvc.ResourceNotFoundException,org.springframework.web.HttpRequestMethodNotSupportedException]: java.lang.IllegalArgumentException: Id must be assignable to Serializable!: to.wysylam.couriersystem.api.entities.User 

[DEBUG] 2017-11-06 18:25:01.055 [http-nio-8080-exec-39] ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.hateoas.Resources<?> org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.data.rest.webmvc.ResourceNotFoundException,org.springframework.web.HttpRequestMethodNotSupportedException]: java.lang.IllegalArgumentException: Id must be assignable to Serializable!: to.wysylam.couriersystem.api.entities.User 

答えて

0

私のコードに間違いがありました。

私は、データベースに関連して定義されたユーザーを持っていないいくつかのアドレス行がある一方で、それは、付与されたのユーザープロパティを取っ定義さResourceProcessor<Resource<Address>>を持っていました。

単純なnullチェックで解決しました。

ケースが閉じられました。

1

多対に@JsonSerialize(as = Address.class)の設定: 問題は、しかし、

UPDATE 2

いくつかの興味深いログ・ダンプ(GET <host>/api/data/addressesを処理中)... there.Obviouslyまだあります1つのクラスは私のケースで私を助けました...

+0

残念ながら私にとってはうまくいきませんでした...他に何ができましたか? –