2017-03-07 56 views
0

我嘗試了json視圖,而不是在使用@Resource的域類的頂部,而是通過創建RestfulController並嘗試使用json視圖呈現該視圖。我在build config中添加了所有相關的依賴關係。當試圖在rest上使用JSON視圖時得到null屬性Controller只是從RestfulController中延伸

我有一個域Post類這樣的(這我不想直接暴露)

class Post implements Serializable { 

    Map comments 
    User user 
    Venue venue 
    String description 
    Rating rating  //should this be an enum? 

    LocalDateTime dateCreated 
    LocalDateTime lastUpdated 

    static belongsTo = [user:User] 
    static hasOne = [rating:Rating] 

    static constraints = { 
     venue nullable:true 
     comments nullable:true 
     description nullable:true 

     rating nullable:true, lazy:false 
    } 

    static mapping = { 
     //set the sort order for Posts - default using newest post first 
     sort dateCreated :"desc" 


    } 
} 

,所以我再創建一個「默認restfulContoller這樣

class PostRestController extends RestfulController { 
    static responseFormats = ["json", "xml"] 

    //constructor - tells rest controller which domain class to scaffold 
    PostRestController() { 
     super (Post) 
    } 
} 

我是這裏不覆蓋任何默認的腳手架方法。

當我使用一個休息客戶端訪問默認(我已經映射/api/posts (resources: postRest UrlMappings)。當我和我的REST客戶端訪問URL我得到了郵政的完全轉儲(包括註釋字段堅持地圖) - 這個看起來像這樣在我休息的客戶 - 一切ok

[ 
    { 
"id": 1, 
"comments": { 
"view": "lovely" 
}, 
"dateCreated": { 
"class": "java.time.LocalDateTime", 
"dayOfMonth": 7, 
"dayOfWeek": { 
"enumType": "java.time.DayOfWeek", 
"name": "TUESDAY" 
}, 
"dayOfYear": 66, 
"hour": 19, 
"minute": 15, 
"month": { 
"enumType": "java.time.Month", 
"name": "MARCH" 
}, 
"monthValue": 3, 
"nano": 263000000, 
"second": 10, 
"year": 2017, 
"chronology": { 
"calendarType": "iso8601", 
"class": "java.time.chrono.IsoChronology", 
"id": "ISO" 
} 
}, 
"description": null, 
"lastUpdated": { 
"class": "java.time.LocalDateTime", 
"dayOfMonth": 7, 
"dayOfWeek": { 
"enumType": "java.time.DayOfWeek", 
"name": "TUESDAY" 
}, 
"dayOfYear": 66, 
"hour": 19, 
"minute": 15, 
"month": { 
"enumType": "java.time.Month", 
"name": "MARCH" 
}, 
"monthValue": 3, 
"nano": 263000000, 
"second": 10, 
"year": 2017, 
"chronology": { 
"calendarType": "iso8601", 
"class": "java.time.chrono.IsoChronology", 
"id": "ISO" 
} 
}, 
"rating": null, 
"user": { 
"id": 1 
}, 
"venue": null 
} 
], 

然後我試圖添加在grails-app/views/postRest夾在此之上JSON意見

我不喜歡這個

model { 
    Post post 
} 

json { 
    comments post.comments 
    description post.description 
    //rating post.rating 
    userWhoPosted "${post?.user}" 
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd") 
    def when = post.dateCreated.format(formatter) 
    created when 
} 

一個非常簡單的模板_post.gson我再補充一個`index.gson」呈現模板

model { 
    List<Post> postList 
} 

//call the template to iterate over the postList to produce the output 
json g.render(postList) 

這使服務器出現這種stracktrace和500錯誤到其他客戶端。如果我註釋掉與用戶有關的行in_post.gson,它一切正常。離開它,它失敗

Caused by: grails.views.ViewRenderException: Error rendering view: null 
    at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:43) 
    at grails.views.mvc.GenericGroovyTemplateView.renderMergedOutputModel(GenericGroovyTemplateView.groovy:73) 
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) 
    at grails.views.mvc.renderer.DefaultViewRenderer.render(DefaultViewRenderer.groovy:111) 
    at grails.artefact.controller.RestResponder$Trait$Helper.internalRespond(RestResponder.groovy:188) 
    at grails.artefact.controller.RestResponder$Trait$Helper.respond(RestResponder.groovy:62) 
    at grails.rest.RestfulController.index(RestfulController.groovy:64) 
    at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96) 
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) 
    at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93) 
    ... 4 

如果我註釋出post.user(和評級參考)它工作正常。但是當我嘗試併發布post.user時,它與上述失敗。有大約確保文檔您查詢拉帶裁判記一個fetch連接 - 所以我試圖提供一個覆蓋,以確保我返回的抓取連接 - 我得到的是空返回給客戶端

class PostRestController extends RestfulController { 
    static responseFormats = ["json", "xml"] 

    //constructor - tells rest controller which domain class to scaffold 
    PostRestController() { 
     super (Post) 
    } 

    def index() { 
     Collection<Post> res = Post.list([fetch:[user:"join",rating:"join"]]) 
     res 
    } 
} 

爲什麼當我這樣做沒有JSON視圖它工作正常,當我使用JSON視圖我不能得到輸出包括引用。我檢查到列表請求,並在調試成功返回列表中 - 但在渲染

打破,如果我能得到這個工作中,Grails JSON意見3.2.6看起來相當不錯

任何人有任何想法,爲什麼這會打破?

回答

1

Aaargh - 認爲這個問題是與jsonViews 1.1.5 - 它沒有準備好java 8 LocalDateTime。

我看到計算器see topic

休眠現在就localdateTime在您的域類蹤 - 工程。但json模板渲染不會,即使你添加了java8插件。

所以我回到域類改變我的LocalDateTime回到日期,也改變了json模板使用舊的SimpleDateTime格式(而不是DateTimeFormatter),並重新運行 - 低,看它的工作。

我會很高興的時候,我們可以說Grails是正確java8準備。

顯然json的意見需要功能啓用這是由於在JSON意見2(認爲這是一個M2現在) - 所以我不得不恢復到Java 7的日期在那之前。

啊呀另一個失去的一天,在雜草。

相關問題