grails restful groovy无法解析符号
我试图通过以下链接学习grails:根据页面的指导创建控制器ProductController后,我得到以下错误:grails restful groovy无法解析符号,grails,groovy,Grails,Groovy,我试图通过以下链接学习grails:根据页面的指导创建控制器ProductController后,我得到以下错误: Unable to resolve class ProductService in ProductController 我是groovy新手,试图通过导入必要的包来解析类引用,但是链接没有显示解析此类的任何导入。ProductController.groovy中没有ProductService的显式导入语句。以下是课程: 产品控制器: package hibernate.exam
Unable to resolve class ProductService in ProductController
我是groovy新手,试图通过导入必要的包来解析类引用,但是链接没有显示解析此类的任何导入。ProductController.groovy中没有ProductService的显式导入语句。以下是课程:
产品控制器:
package hibernate.example
import groovy.transform.CompileStatic
import grails.rest.*
import grails.converters.*
@CompileStatic
class ProductController extends RestfulController {
static responseFormats = ['json', 'xml']
ProductController() {
super(Product)
}
ProductService productService
def search(String q, Integer max ) {
if (q) {
respond productService.findByNameLike("%${q}%".toString(), [max: Math.min( max ?: 10, 100)])
}
else {
respond([])
}
}
}
ProductControllerSpec:
package hibernate.example
import org.grails.testing.GrailsUnitTest
import spock.lang.Specification
@SuppressWarnings('MethodName')
class ProductControllerSpec extends HibernateSpec implements ControllerUnitTest<ProductController> {
def setup() {
}
def cleanup() {
}
static doWithSpring = {
jsonSmartViewResolver(JsonViewResolver)
}
void 'test the search action finds results'() {
given:
controller.productService = Stub(ProductService) {
findByNameLike(_, _) >> [new Product(name: 'Apple', price: 2.0)]
}
when: 'A query is executed that finds results'
controller.search('pp', 10)
then: 'The response is correct'
response.json.size() == 1
response.json[0].name == 'Apple'
}
}
package hibernate.example
导入org.grails.testing.GrailsUnitTest
导入spock.lang.Specification
@SuppressWarnings('MethodName')
类ProductControllerSpec扩展HibernateSpect实现ControllerUnitTest{
def设置(){
}
def cleanup(){
}
静态定位销弹簧={
jsonSmartViewResolver(JsonViewResolver)
}
void“测试搜索操作查找结果”(){
鉴于:
controller.productService=存根(productService){
findByNameLike(,)>>[新产品(名称:'Apple',价格:2.0)]
}
当:“执行查找结果的查询”
控制器搜索('pp',10)
然后:“回答是正确的”
response.json.size()==1
response.json[0]。name==“苹果”
}
}
该教程中似乎完全缺少该类。。。您可以从包含教程源代码的GitHub repo获取服务的源代码,不用担心,很高兴我能提供帮助。享受吧!