2017-07-17 241 views
1

我想在我的Spring Boot項目中使用Spring Data Rest,但我遇到了困難。 我正在使用Spring啓動版本2.0.0.M2。我也試過版本1.5.4.RELEASE但得到Spring Boot和Spring Data Rest

我使用以下進口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要求和他們的工作第一所以實體映射到我的數據庫表是正確的

任何人都可以提供任何協助,爲什麼發生這種情況? 我檢查我的數據庫映射,他們似乎是確定

感謝 達明

+0

'java.lang.String中不能被強制轉換爲的java.lang .Long'您發送的數據不正確。 – EpicPandaForce

+0

是的,但彈簧數據應該照顧這一點。我有一個簡單的數據庫對象相同的設置,它工作正常 – Damien

+0

春天的數據不會照顧你發送一個字符串長字段,反之亦然。 – EpicPandaForce

回答

1

我認爲你是不發送「ID」作爲您的JSON的一部分。您可能認爲id是自動創建的,並假定您不必發送。如果是這種情況(即,您不想在您的JSON對象中發送id),請將@JsonIgnore添加到實體定義中的id中。如果你這樣做,當你查詢時你也不會在返回對象中包含Id字段。如果你想要id,請在你的JSON中發送一個null或「」(空字符串)。有時候,Spring啓動可能會忽略這些空白字段。應該有另一個@Json ...註釋來指示它不要忽略空值/空值。我無法把它放在首位,你必須Google。

+0

該註釋是@JsonIgnoreProperties(ignoreUnknown = true) - 不幸的是沒有改變 - 相同的輸出 – Damien