Grails 2.3.4에서 RESTful 컨트롤러를 만들려고합니다. 그러나 나는 POST를 시도 할 때마다 인덱스 메소드를 항상 쳤다. save()가 작동하도록하는 유일한 방법은 인덱스 메소드를 삭제하는 것입니다. 이것은 나에게 어떤 의미가되지 않습니다.Grails RESTful 컨트롤러. POST가 save() 대신 index()를 호출합니다.
나는 모두 RestfulController 슈퍼 클래스 확장 시도 : http://grails.org/doc/2.3.4/guide/webServices.html#extendingRestfulController
을 그리고 내 자신의 방법을 구현 : 아직 http://grails.org/doc/2.3.4/guide/webServices.html#restControllersStepByStep
, 나는 내 URL로 게시 할 때, 그것은 단지가 하나인지, 인덱스 방법 안타를 RestfulController에서 확장 중이거나 내 자신의 메서드에서.
내가 가진 내가 json: ['application/json', 'text/json'],
내가 추가하고 내 컨트롤러에서 static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
을 제거하려고했습니다
내 마임 유형에 정의가 UrlMappings.groovy 에 "/api/lab"(controller: "lab")
.
하지만 내가 무엇을해도 save()를 치는 것처럼 보일 수 없습니다. 이것은 내 컨트롤러는 확장 RestfulController
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()
}
}
입니다 :
이 내가 내 자신의 컨트롤러 구현하고 내 컨트롤러
class LabController extends RestfulController {
static responseFormats = ['json']
LabController() {
super(PatientLab)
}
}
하하, 와우, 네 말이 맞아, 내가 그걸 망쳐 놓은 것을 믿을 수 없어. 나는 내가 문서를 정확하게 따라 갔다고 말했을 때 내가 거짓말했다고 생각한다 :/ 도움을 많이 주셔서 감사합니다! –
@RobLouie 도움이 되었기 때문에 기꺼이, 나는 몇 번이나 그 설정을 놓쳤습니다. –