I am trying to create a RESTful controller in Grails 2.3.4. I follow the documentation for sure, however, when I try to execute POST, I always press the index method. The only way to get save () is to remove the index method. That makes no sense to me.
I tried both extensions of the RestfulController superclass: http://grails.org/doc/2.3.4/guide/webServices.html#extendingRestfulController
And implementing my own methods: http://grails.org/doc/2.3.4/guide/webServices.html#restControllersStepByStep
And yet, when I send a message to my url, it only gets into the index method, regardless of whether it is one of the remaining RestfulController components or my own method.
I have "/api/lab"(controller: "lab")in UrlMappings.groovy I have json: ['application/json', 'text/json'],, defined in my mime types
I tried to add and remove static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]from my controller.
But no matter what I do, I cannot hit save ().
This is my controller when I implement my own controller:
class LabController {
static responseFormats = ['json']
def userService
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
Patient patient = userService.currentUser.patient
render PatientLab.findAllByPatientAndType(patient, LabType.findByName(LabType.LAB_TYPE_INR)) as JSON
}
@Transactional
def save() {
Patient patient = userService.currentUser.patient
LabType inrType = LabType.findByName(LabType.LAB_TYPE_INR)
boolean labAlreadyExists = PatientLab.findByPatientAndLabDateAndType(patient, request.JSON.labDate, inrType)
if (labAlreadyExists) {
render {result: "Lab already exists"} as JSON
return
}
PatientLab patientLab = new PatientLab()
patientLab.patient = patient
patientLab.origin = LabOrigin.MOBILE_APP
patientLab.type = inrType
patientLab.result = request.JSON.result
patientLab.measurement = UnitType.findByUnit(UnitType.UNIT_TYPE_NONE)
patientLab.labDate = new Date()
}
}
This is my controller when extending RestfulController:
class LabController extends RestfulController {
static responseFormats = ['json']
LabController() {
super(PatientLab)
}
}