2016-07-18 147 views
4

我收到一個錯誤 - '類com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer沒有默認值沒有arg)構造函數',而我正試圖調用postang請求。當我調用方法時,它會進入錯誤塊。類com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer沒有默認的(無參數)構造函數

Restangular.all('tests').post($scope.test).then(function (data) { 
        $scope.test.id = data.id; 
        $location.path($location.path() + data.id).replace(); 
       }, function (error) { 
        $scope.exceptionDetails = validationMapper(error); 
       }); 

我使用傑克遜 - 數據類型 - 約達 - 2.6.5

在此方法中使用的實體類如下 -

@Data 
@JsonInclude(JsonInclude.Include.NON_NULL) 
@Entity 
@Table(name = "Test") 
@EqualsAndHashCode(of = "id", callSuper = false) 
@ToString(exclude = {"keywords", "relevantObjectIds"}) 
public class Test { 
    @Id 
    @Column(unique = true, length = 36) 
    private String id; 

    @NotBlank 
    @NotNull 
    private String name; 

    @Transient 
    private List<Testabc> Testabcs = new ArrayList<>(); 

} 

實體類在上述實體testAbc的使用類如下

@Data 
@JsonInclude(JsonInclude.Include.NON_NULL) 
@Slf4j 
@Entity 
@Table(name = "Test_abc") 
@EqualsAndHashCode(of = "id", callSuper = false) 
public class Testabc{ 
    @Id 
    @Column(unique = true, length = 36) 
    @NotNull 
    private String id = UUID.randomUUID().toString(); 

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") 
    @JsonDeserialize(using = DateTimeDeserializer.class) 
    @JsonSerialize(using = DateTimeSerializer.class) 
    private DateTime createdOn; 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "Id") 
    @NotNull 
    private t1 pid; 

    private long originalSize; 
} 

最後資源類我在哪裏請求創建測試數據 -

@ApiOperation(value = "Create new Test", notes = "Create a new Test and return with its unique id", response = Test.class) 
    @POST 
    @Timed 
    public Test create(Test newInstance) { 
     return super.create(newInstance); 
    } 

我已嘗試添加該 @JsonIgnoreProperties(ignoreUnknown =真)註釋上的實體類,但它不工作。

任何人都可以幫助解決這個問題?

+0

[joda.time.DateTime反序列化錯誤]的可能的複製(http://stackoverflow.com/questions/22643367/joda-time-datetime-deserialization-error) – Morfic

+0

沒有就沒有duplicacy – Geetanjali

+0

這是否意味着從解釋鏈接問題的答案和'mapper.registerModule(new JodaModule())'的建議解決方案;'不適合你? – Morfic

回答

1

綜觀DateTimeDeserializer最新的來源,你可以很容易地看到,它並沒有一個無參數的構造函數,這似乎是由框架是必需的。這兩個鏈接的問題也表示:joda.time.DateTime deserialization error & Jackson, Retrofit, JodaTime deserialization

既然你希望只使用一個基於註釋的解決方案,可能的解決方法是創建自己的解串器擴展了DateTimeDeserializer,並提供一個也沒有參數的構造函數。

1)MyDateTimeSerializer

import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig; 
import com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer; 
import org.joda.time.DateTime; 

public class MyDateTimeDeserializer extends DateTimeDeserializer { 
    public MyDateTimeDeserializer() { 
     // no arg constructor providing default values for super call 
     super(DateTime.class, FormatConfig.DEFAULT_DATETIME_PARSER); 
    } 
} 

2)AClass使用自定義解串器

import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 
import com.fasterxml.jackson.databind.annotation.JsonSerialize; 
import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer; 
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 

public class AClass { 

    @JsonSerialize(using = DateTimeSerializer.class) // old serializer 
    @JsonDeserialize(using = MyDateTimeDeserializer.class) // new deserializer 
    private DateTime createdOn = DateTime.now(DateTimeZone.UTC); // some dummy data for the sake of brevity 

    public DateTime getCreatedOn() { 
     return createdOn; 
    } 

    public void setCreatedOn(DateTime createdOn) { 
     this.createdOn = createdOn; 
    } 
} 

3)單位測試

import com.fasterxml.jackson.databind.ObjectMapper; 
import org.junit.Test; 

import static org.hamcrest.CoreMatchers.equalTo; 
import static org.hamcrest.CoreMatchers.is; 
import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertThat; 

public class ATest { 
    @Test 
    public void testSomeMethod() throws Exception { 
     // Jackson object mapper to test serialization/deserialization 
     ObjectMapper objectMapper = new ObjectMapper(); 

     // our object 
     AClass initialObject = new AClass(); 

     // serialize it 
     String serializedObject = objectMapper.writeValueAsString(initialObject); 

     // deserialize it 
     AClass deserializedObject = objectMapper.readValue(serializedObject, AClass.class); 

     // check that the dates are equal (no equals implementation on the class itself...) 
     assertThat(deserializedObject.getCreatedOn(), is(equalTo(initialObject.getCreatedOn()))); 
    } 
} 
+0

謝謝,但它不起作用,仍然有同樣的錯誤 – Geetanjali

+0

@Geetanjali雖然這是可能的,但是你不可能在你改變某些東西之後得到同樣的異常,因爲這意味着你的修改沒有任何影響。你可以請**清理**你的工作區/輸出文件夾,**重建**你的應用程序,然後重試並共享異常消息和堆棧跟蹤? – Morfic

+0

感謝您的迴應,但我已經嘗試了您的建議,並確保我已構建並清理應用程序。還有一件事是我沒有任何堆棧跟蹤,因爲代碼在甚至去到java代碼之前就會中斷,它只是調用Restangular的錯誤塊,即'Class com.fasterxml.jackson.datatype.joda.deser.DateTimeDeserializer has沒有默認(無參數)構造函數「。 – Geetanjali

相關問題