嗨,我有一个 grails restful 代码。我确实完善了 Json 获取和删除请求。我的问题是:我想用 json 做一个 Put 或 post 请求,但是如果我使用一个程序来做它有一个错误,我做不到!这是我的代码:一类:
package catalogo.json
class Catalogo {
String nombre
String descripcion
String url
Set <Parametros>parametros =[]
static hasMany = [parametros:Parametros]
int numeroParametros = parametros.size()
}
和其他类:
package catalogo.json
class Parametros {
String tipoParametro
String json
static constraints = {
tipoParametro(nullable:true)
json(nullable:true)
}
}
我不知道如何将我的 json 放入请求中。这是我放的错误。
示例:
我提出 POST 请求。正文application/json
(或text/json
相同的错误) Charset-utf-8
{"descripcion": "bla", "nombre" : "lalala", "numeroParametros":3, "parametros":[{
"tipoParametro":"string", "json":"bla"}],"url":"http://www.google.com"}
这里是错误:
Could not create new Catalogo due to errors:
grails.validation.ValidationErrors: 3 errors
Field error in object 'catalogo.json.Catalogo' on field 'descripcion': rejected value [null]; codes [catalogo.json.Catalogo.descripcion.nullable.error.catalogo.json.Catalogo.descripcion,catalogo.json.Catalogo.descripcion.nullable.error.descripcion,catalogo.json.Catalogo.descripcion.nullable.error.java.lang.String,catalogo.json.Catalogo.descripcion.nullable.error,catalogo.descripcion.nullable.error.catalogo.json.Catalogo.descripcion,catalogo.descripcion.nullable.error.descripcion,catalogo.descripcion.nullable.error.java.lang.String,catalogo.descripcion.nullable.error,catalogo.json.Catalogo.descripcion.nullable.catalogo.json.Catalogo.descripcion,catalogo.json.Catalogo.descripcion.nullable.descripcion,catalogo.json.Catalogo.descripcion.nullable.java.lang.String,catalogo.json.Catalogo.descripcion.nullable,catalogo.descripcion.nullable.catalogo.json.Catalogo.descripcion,catalogo.descripcion.nullable.descripcion,catalogo.descripcion.nullable.java.lang.String,catalogo.descripcion.nullable,nullable.catalogo.json.Catalogo.descripcion,nullable.descripcion,nullable.java.lang.String,nullable]; arguments [descripcion,class catalogo.json.Catalogo]; default message [La propiedad [{0}] de la clase [{1}] no puede ser nulo]
Field error in object 'catalogo.json.Catalogo' on field 'nombre': rejected value [null]; codes [catalogo.json.Catalogo.nombre.nullable.error.catalogo.json.Catalogo.nombre,catalogo.json.Catalogo.nombre.nullable.error.nombre,catalogo.json.Catalogo.nombre.nullable.error.java.lang.String,catalogo.json.Catalogo.nombre.nullable.error,catalogo.nombre.nullable.error.catalogo.json.Catalogo.nombre,catalogo.nombre.nullable.error.nombre,catalogo.nombre.nullable.error.java.lang.String,catalogo.nombre.nullable.error,catalogo.json.Catalogo.nombre.nullable.catalogo.json.Catalogo.nombre,catalogo.json.Catalogo.nombre.nullable.nombre,catalogo.json.Catalogo.nombre.nullable.java.lang.String,catalogo.json.Catalogo.nombre.nullable,catalogo.nombre.nullable.catalogo.json.Catalogo.nombre,catalogo.nombre.nullable.nombre,catalogo.nombre.nullable.java.lang.String,catalogo.nombre.nullable,nullable.catalogo.json.Catalogo.nombre,nullable.nombre,nullable.java.lang.String,nullable]; arguments [nombre,class catalogo.json.Catalogo]; default message [La propiedad [{0}] de la clase [{1}] no puede ser nulo]
Field error in object 'catalogo.json.Catalogo' on field 'url': rejected value [null]; codes [catalogo.json.Catalogo.url.nullable.error.catalogo.json.Catalogo.url,catalogo.json.Catalogo.url.nullable.error.url,catalogo.json.Catalogo.url.nullable.error.java.lang.String,catalogo.json.Catalogo.url.nullable.error,catalogo.url.nullable.error.catalogo.json.Catalogo.url,catalogo.url.nullable.error.url,catalogo.url.nullable.error.java.lang.String,catalogo.url.nullable.error,catalogo.json.Catalogo.url.nullable.catalogo.json.Catalogo.url,catalogo.json.Catalogo.url.nullable.url,catalogo.json.Catalogo.url.nullable.java.lang.String,catalogo.json.Catalogo.url.nullable,catalogo.url.nullable.catalogo.json.Catalogo.url,catalogo.url.nullable.url,catalogo.url.nullable.java.lang.String,catalogo.url.nullable,nullable.catalogo.json.Catalogo.url,nullable.url,nullable.java.lang.String,nullable]; arguments [url,class catalogo.json.Catalogo]; default message [La propiedad [{0}] de la clase [{1}] no puede ser nulo]
在这里编辑我的控制器:
package catalogo.json
import grails.converters.JSON
class CatalogoController {
def index = {
switch(request.method){
case "POST":
def catalogo = new Catalogo(params.catalogo)
if(catalogo.save()){
response.status = 201 // Created
render catalogo as JSON
}
else{
response.status = 500 //Internal Server Error
render "Could not create new Airport due to errors:\n
${catalogo.errors}"
}
break
case "GET":
if(params.id){render Catalogo.findById(params.id) as JSON}
else{render Catalogo.list() as JSON}
break
case "PUT":
def catalogo = Catalogo.findById(params.catalogo.id)
catalogo.properties = params.catalogo
if(catalogo.save()){
response.status = 200 // OK
render catalogo as JSON
}
else{
response.status = 500 //Internal Server Error
render "Could not create new Catalogo due to errors:\n ${catalogo.errors}"
}
break
case "DELETE":
if(params.id){
def catalogo = Catalogo.findById(params.id)
if(catalogo){
catalogo.delete()
render "Successfully Deleted."
}
else{
response.status = 404 //Not Found
render "${params.id} not found."
}
}
else{
response.status = 400 //Bad Request
render """DELETE request must include the ID code
Example: /rest/catalogo/id
"""
}
break
}
}
def list = {
if(!params.max) params.max = 10
def list = Catalogo.list(params)
withFormat{
html{
return [catalogoList:list]
}
json{
render list as JSON
}
}
}
}
谢谢