春天引导和Spring数据休息(Spring Boot and Spring Data Rest)

2019-09-27 04:45发布

我想使用Spring数据安息我的春节,启动项目,但我遇到了困难。 我使用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>

我的数据库对象如下

    @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数据库)

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;

repository类

    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>{

}

当我试图进入春季数据端点和这个对象 - 我得到以下错误

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通话使用标准服务类和他们的工作第一次这么实体映射到我的数据库表是正确的

任何人都可以提供关于为什么发生这种情况的任何援助? 我检查我的数据库映射,他们似乎是确定

感谢达米安

Answer 1:

我想,你是不是送“身份证”作为JSON的一部分。 你可能认为可能id为自动创建的,并且假设你没有送。 如果是这样的话(即你不想在你的JSON对象发送ID)添加@JsonIgnore在实体定义ID。 如果你这样做,你不会得到包含在返回的对象,当你查询以及Id字段。 如果你要ID,在你的JSON发送一个空或“”(空字符串)。 有时候,春启动可能会忽略这些空字段。 还应该有一个@Json ...注释指示它不要忽略空/空值。 我无法得到它的上面我的头,你必须谷歌吧。



Answer 2:

根据这个帖子- https://github.com/spring-projects/spring-boot/issues/9756从2.0.0.M1改变弹簧引导版本2.0.0.BUILD-快照解决问题



文章来源: Spring Boot and Spring Data Rest