首页 文章

Spring Boot和Spring Data Rest

提问于
浏览
1

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

我在我的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)

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和此对象的 endpoints 时 - 我收到以下错误

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"])

Note: 我尝试使用标准服务类进行JPA调用,并且他们第一次工作,因此映射到我的数据库表的实体是正确的

任何人都可以提供任何帮助,说明为什么会这样吗?我检查了我的数据库映射,看起来没问题

谢谢Damien

2 回答

  • 0

    我认为你不是发送'id'作为你的JSON的一部分 . 您可能已经假定id是自动创建的,并且假设您想要在JSON对象中发送id)在您的Entity定义中将id添加 @JsonIgnore 到id . 如果这样做,在查询时也不会在返回对象中包含Id字段 . 如果你想要id,在你的JSON中发送一个null或“”(空字符串) . 有时,Spring引导可能会忽略这些空字段 . 应该有另一个@Json ...注释来指示它不要忽略null / empty值 . 我无法理解它,你必须谷歌它 .

  • 1

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

相关问题