I know i am a bit late but i've been searching for the solution for long and didnt find anything. After a lot of effort i found that sending jackson "Object" with "@BODY" is not helpful, when i changed the the type to "String" it worked for me.
Here is my Code
def save(@Body String JSON){
def result = [:]
ObjectMapper objectMapper = new ObjectMapper();
//convert json string to object
def obj = objectMapper.readValue(JSON, Course.class);
println(obj)
Course course = new Course(name: obj?.name, pre: obj?.pre,
regno: obj?.regno, enrolled: obj?.enrolled)
course.validate()
if(course.hasErrors()){
println("Error: "+course.errors)
result.put("Error is: ",course.errors)
return result;
}
course.save(flush:true,failOnError: true)
result.put("Message","Successfully Created")
result.put("Result",course)
return HttpResponse.created(result)
}
Passing it to ObjectMapper and then converting it from JSON string to Java Object worked for me.
Json string that i passed is as follow:
{
"name" : "Data Structures",
"pre" : "Computer Programming",
"regno" : "249",
"enrolled" : "90"
}
Here is the storing of data before and after change in database:
+----+---------+------------------------+-------------------------------+----+
| id | version | name | pre | regno |enrolled |
+----+---------+------------------------+-------------------------------+----+
| 1 | 0 | "Computer Programming" | "Introduction to Programming"| "233"|"26"|
| 2 | 0 | Data Structures | Computer Programming | 249 | 90 |
+----+---------+------------------------+-------------------------------+----+
Hope the answer helps out anyone who is looking for alternate solution to above solution.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…