2014-10-10 1 views
4

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) 
} 
} 

답변

3

을 따라서 allowedMethods 단지를 얻기에서 당신을 막을 것입니다 GET 요청을 저장하려면(), Restful Mappings 대신 매핑하는 대신

"/api/lab"(controller: "lab") 

사용 API /로 POST를 매핑합니다

"/api/labs"(resources: "lab") 

/를 save() 방법에 실험실. 당신의 나머지 컨트롤러에서 복수형을 사용하고 싶지 않다면 대신 resource을 사용할 수 있습니다. 그러나 대부분은 복수형을 사용합니다. 내가 지원하는 메소드 목록을 갖고 싶습니다. includes:['index', 'show']을 사용하면 몇 가지 작업을 쉽게 지원할 수 있습니다.

+0

하하, 와우, 네 말이 맞아, 내가 그걸 망쳐 놓은 것을 믿을 수 없어. 나는 내가 문서를 정확하게 따라 갔다고 말했을 때 내가 거짓말했다고 생각한다 :/ 도움을 많이 주셔서 감사합니다! –

+0

@RobLouie 도움이 되었기 때문에 기꺼이, 나는 몇 번이나 그 설정을 놓쳤습니다. –

관련 문제