2016-08-29 38 views
1

我正在使用駱駝與Spring Boot。在基本上記錄消息正文的服務路由實現期間,我看到一個如下所示的錯誤。駱駝複雜類型到字符串typeconverter錯誤

No converter found capable of converting from type [com.example.Book] to type [java.lang.String] 

我的路線是:

from(REST_ENDPOINT_URI) 
    .log("${headers}") 
    .log("${body}") 

和我在日誌正文行錯誤。

我的問題是預期的行爲?爲什麼駱駝只是調用Book對象的toString方法。而且,如果這是預期的行爲,那麼我需要一個字符串轉換器用於每個新的複雜類型?

+0

did you try .log(「$ {body.toString}」)?假設定義了toString()。 –

回答

0

您可以創建一個可重現問題的可運行示例嗎?這絕對是駱駝通過調用toString來處理的一種場景。

舉例來說,你可以測試它:

@Component 
public class DemoRouteBuilder extends RouteBuilder { 


    @Override 
    public void configure() throws Exception { 
    from("timer:sender?delay=5s&period=3s") 
     .setBody(constant(new Book("Lord of the Rings", "J.R.R. Tolkien"))) 
     .log("${body}!"); 
    } 

    public static class Book { 
    private final String title; 
    private final String author; 

    public Book(String title, String author) { 
     this.title = title; 
     this.author = author; 
    } 

    @Override 
    public String toString() { 
     return "Book{" + 
      "title='" + title + '\'' + 
      ", author='" + author + '\'' + 
      '}'; 
    } 
    } 
} 

這將導致以下的輸出:

2016年8月30日11:57:49.802 INFO 8778 --- [計時器:// sender] route1:Book {title ='指環王',作者='JRR托爾金'}!

2016-08-30 11:57:52.792 INFO 8778 --- [timer:// sender] route1:Book {title ='指環王',作者='J.R.R。托爾金'}!

2016-08-30 11:57:55.795 INFO 8778 --- [timer:// sender] route1:Book {title ='指環王',作者='J.R.R。托爾金'}!