Spring Boot 和 Spring Data Rest [英] Spring Boot and Spring Data Rest

查看:86
本文介绍了Spring Boot 和 Spring Data Rest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Spring Boot 项目中使用 Spring Data Rest,但我遇到了困难.我使用的是 Spring Boot 版本 2.0.0.M2.我也尝试过 1.5.4.RELEASE 版本但得到了同样的错误

I want to use Spring Data Rest in my Spring Boot project but I am running into difficulty. I am using Spring boot version 2.0.0.M2. I also tried version 1.5.4.RELEASE but got the same error

我在我的 pom 中使用以下导入

I am using the following imports in my pom

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

我的DB对象如下

    @Entity
@Table(name = "T_PUBLICATION")
public class PublicationDBO implements Serializable{

    private static final long serialVersionUID = -8883649751668748295L;

    @Id
    @Column(name = "id")
    private Long id;    
    @Column(name = "publication_name")
    private String publicationName; 
    @Column(name = "number_of_pages")
    private Long numberOfPages; 
    @Column(name = "storage_key")
    private String storageKey;  
    @Column(name = "processing_complete")
    private Boolean processingComplete; 
    @Column(name = "date_added")
    private LocalDateTime dateAdded;    
    @Column(name = "date_updated")
    private LocalDateTime dateUpdated;

}

数据库 SQL (MySQL DB)

Database SQL (MySQL DB)

CREATE TABLE `T_PUBLICATION` (
  `id` int(11) NOT NULL,
  `publication_name` varchar(255) NOT NULL,
  `number_of_pages` int(11) NOT NULL,
  `storage_key` varchar(255) NOT NULL,
  `processing_complete` tinyint(1) NOT NULL,
  `date_added` datetime NOT NULL,
  `date_updated` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

存储库类

    import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.dao.vo.PublicationDBO;

@RepositoryRestResource(collectionResourceRel = "publication", path = "publication")
public interface PublicationRepository extends PagingAndSortingRepository<PublicationDBO, Long>{

}

当我尝试访问 Spring Data 和此对象的端点时 - 我收到以下错误

When I tried to access the endpoints for Spring Data and this object - I get the following error

2017-07-17 16:03:28 [http-nio-8080-exec-1] DEBUG org.hibernate.SQL - select publicatio0_.id as id1_1_0_, publicatio0_.date_added as date_add2_1_0_, publicatio0_.date_updated as date_upd3_1_0_, publicatio0_.number_of_pages as number_o4_1_0_, publicatio0_.processing_complete as processi5_1_0_, publicatio0_.publication_name as publicat6_1_0_, publicatio0_.storage_key as storage_7_1_0_ from T_PUBLICATION publicatio0_ where publicatio0_.id=?
Hibernate: select publicatio0_.id as id1_1_0_, publicatio0_.date_added as date_add2_1_0_, publicatio0_.date_updated as date_upd3_1_0_, publicatio0_.number_of_pages as number_o4_1_0_, publicatio0_.processing_complete as processi5_1_0_, publicatio0_.publication_name as publicat6_1_0_, publicatio0_.storage_key as storage_7_1_0_ from T_PUBLICATION publicatio0_ where publicatio0_.id=?
2017-07-17 16:03:28 [http-nio-8080-exec-1] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: java.lang.String cannot be cast to java.lang.Long; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.lang.String cannot be cast to java.lang.Long (through reference chain: org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->com.dao.vo.PublicationDBO["publicationName"])
2017-07-17 16:03:28 [http-nio-8080-exec-1] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: java.lang.String cannot be cast to java.lang.Long; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.lang.String cannot be cast to java.lang.Long (through reference chain: org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1["content"]->com.dao.vo.PublicationDBO["publicationName"])

注意:我使用标准服务类尝试了 JPA 调用,它们第一次工作,因此实体映射到我的数据库表是正确的

Note: I tried the JPA calls using a standard service class and they worked first time so the entity mapping to my database tables is correct

任何人都可以就为什么会发生这种情况提供任何帮助吗?我检查了我的数据库映射,它们似乎没问题

Can anyone offer any assistance on why this is happening? I have checked my database mappings and they appear to be ok

谢谢达米安

推荐答案

根据这篇文章 -https://github.com/spring-projects/spring-boot/issues/9756将 Spring Boot 版本从 2.0.0.M1 更改为 2.0.0.BUILD-SNAPSHOT 已解决问题

According to this post -https://github.com/spring-projects/spring-boot/issues/9756 Changing the Spring Boot version from 2.0.0.M1 to 2.0.0.BUILD-SNAPSHOT has resolved the issue

这篇关于Spring Boot 和 Spring Data Rest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆