grails - grails restful groovy 无法解析符号
问题描述
我正在尝试通过以下链接学习 grails:https : //guides.grails.org/rest-hibernate/guide/index.html 并根据页面指南创建控制器 ProductController 后,我收到以下错误:
Unable to resolve class ProductService in ProductController
我是 groovy 的新手,并试图通过导入必要的包来解析类引用,但链接没有显示任何导入来解析此类。ProductController.groovy 中没有 ProductService productService 的显式导入语句。以下是这些类: ProductController:
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([])
}
}
}
产品控制器规格:
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'
}
}
解决方案
推荐阅读
- shell - Groovy Jenkins 脚本中的 sh 命令
- c# - 使用 Modulus 进行偶数/奇数循环计数
- python - Pandas describe() 函数不会在 PyCharm 上打印任何内容
- powershell - 如何根据过滤器选择特定的子串长度
- c# - 无法在任务之间做出决定
, IActionResult 和 ActionResult - asp.net - 没有 Kerberos 委派的同一域中的服务之间的 Windows 身份验证
- javascript - 对象中声明的函数不是函数
- redux - Redux mapStateToProps() 在使用 Next.js 返回后不呈现
- arrays - 如何将 collection'1/system.object 变成 Invoke-WebRequest 可以接受的东西?
- php - 如何让 postgresql 将反斜杠解释为绑定值中的转义字符?