【发布时间】:2014-12-22 14:44:56
【问题描述】:
我是初学者。所以请多多包涵。我有一个域类和一个控制器和一个服务。我创建了两种方法;一种是获取列表,另一种是获取参数并返回参数以检查它是否接受值。但我不知道如何调用该函数。正如我曾经输入 localhost:8080/projectname/controllername/methodname。它返回 404 未找到视图。 这是代码 包是testone
域:ReadData.groovy
package testone
import grails.rest.*;
class ReadData {
String customername;
String fathername;
static constraints = {
customername blank: false;
fathername blank:false;
}
}
控制器:ReadDataController.groovy
package testone
import grails.converters.JSON
import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
@Transactional(readOnly = true)
class ReadDataController {
static responseFormats = ['json', 'xml']
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def ReadDataService readDataService;
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond ReadData.list(params), [status: OK]
}
@Transactional
def save(ReadData readDataInstance) {
if (readDataInstance == null) {
render status: NOT_FOUND
return
}
readDataInstance.validate()
if (readDataInstance.hasErrors()) {
render status: NOT_ACCEPTABLE
return
}
readDataInstance.save flush:true
respond readDataInstance, [status: CREATED]
}
@Transactional
def update(ReadData readDataInstance) {
if (readDataInstance == null) {
render status: NOT_FOUND
return
}
readDataInstance.validate()
if (readDataInstance.hasErrors()) {
render status: NOT_ACCEPTABLE
return
}
readDataInstance.save flush:true
respond readDataInstance, [status: OK]
}
@Transactional
def delete(ReadData readDataInstance) {
if (readDataInstance == null) {
render status: NOT_FOUND
return
}
readDataInstance.delete flush:true
render status: NO_CONTENT
}
def readList(){
readDataService.testList();
}
def getName(String name){
render name;
}
}
服务:ReadDataService.groovy
package testone
import grails.transaction.Transactional
@Transactional
class ReadDataService {
def serviceMethod() {
}
def testList(){
return ReadData.list();
}
}
引导:
import testone.ReadData
class BootStrap {
def init = { servletContext ->
new ReadData(customername: 'NameOne',fathername: 'FnameOne').save();
new ReadData(customername: 'NameTwo',fathername: 'FnameTwo').save();
}
def destroy = {
}
}
请帮助我调用这些函数或直接告诉我使用服务方法的方式。我想在 android 应用中使用 grails 服务。
【问题讨论】:
-
您如何称呼您的控制器?您能否包含您正在使用的确切 URL(注意 CapiTaLiZaTiOn)以及您发送的任何 HTTP 标头?
-
控制器类定义应该是
class ReadDataController extends RestfulController {