Grails 2.4具有复合id的REST域

时间:2014-06-30 16:12:53

标签: json grails

提前感谢任何建议。我有一个遗留数据库,并且在使用REST JSON进行列表操作时遇到了问题。

数据库表:

1) vm_properties (to keep key-value properties)
    vm_id int(25) PK 
    vm_key varchar(25) PK 
    vm_value longtext

2) vms table (I guess it should have been vm but as I said it is legacy exxisting db I need to expose CRUD operations using REST-JSON)
    vm_id int(11) AI PK 
    user_id int(11) 
   (.. there are other fields but I guess irrelevant for this problem)

域类:

1) VmProperties:

class VmProperties implements Serializable {
        Integer vm_id
        String vm_key
        String vm_value

        int hashCode() {
            def builder = new HashCodeBuilder()
            builder.append vm_id
            builder.append vm_key
            builder.toHashCode()
        }

        boolean equals(other) {
            if (other == null) return false
            def builder = new EqualsBuilder()
            builder.append vm_id, other.vm_id
            builder.append vm_key, other.vm_key
            builder.isEquals()
        }

        static belongsTo = [Vms]

        static mapping = {
            table 'vm_properties'
            id composite: ["vm_id", "vm_key"]
            version false
        }

        static constraints = {
            vm_value nullable: true
        }
    }

2) Vms:
class Vms {
    Integer vmId
    Integer user_id

    static hasMany = [vmPropertieses: VmProperties]

    static constraints = {
        user_id blank:false, nullable:false
    }

    static mapping = {
        table 'vms'
        version false
        id name:'vmId', column:'vm_id', sqlType:'int'
        vmId insertable:false, updateable:false

    }
}

生成的控制器:

1) vm_properties
class VmPropertiesController extends RestfulController {
    static responseFormats = ['json', 'xml'] 
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond VmProperties.list(params), model:[vmPropertiesInstanceCount: VmProperties.count()]
    }

    def show(VmProperties vmPropertiesInstance) {
        respond vmPropertiesInstance
    }
}

2) VmsController - nothing special... looks same as VmPropertiesController

我可以执行/ testapp / vms并获得JSON中的vms列表。没有问题。但是,当我执行/ testapp / vmProperties时,我收到此错误:没有这样的属性:vmId ....我可以看到该属性就在那一行。

    Application starting ... 
    current environment: DEVELOPMENT
    now running in DEV mode.
....errors.GrailsExceptionResolver MissingPropertyException occurred when processing request: [GET] /passfarm/VmProperties
No such property: vmId for class: org.codehaus.groovy.grails.web.json.JSONObject$Null. Stacktrace follows:
groovy.lang.MissingPropertyException: No such property: vmId for class: org.codehaus.groovy.grails.web.json.JSONObject$Null
    at testapp.domain.VmProperties.equals(VmProperties.groovy:30)
    at grails.converters.JSON.value(JSON.java:176)
    at grails.converters.JSON.convertAnother(JSON.java:162)
    at grails.converters.JSON.value(JSON.java:202)
    at grails.converters.JSON.render(JSON.java:134)
    at grails.rest.render.json.JsonCollectionRenderer.renderJson(JsonCollectionRenderer.groovy:48)
    at org.grails.plugins.web.rest.render.json.DefaultJsonRenderer.renderJson(DefaultJsonRenderer.groovy:110)
    at org.grails.plugins.web.rest.render.json.DefaultJsonRenderer.render(DefaultJsonRenderer.groovy:91)
    at org.grails.plugins.web.rest.api.ControllersRestApi.respond(ControllersRestApi.groovy:177)
    at org.grails.plugins.web.rest.api.ControllersRestApi.respond(ControllersRestApi.groovy:75)
    at testapp.controller.VmPropertiesController.$tt__index(VmPropertiesController.groovy:25)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

0 个答案:

没有答案