grails restful groovy не может разрешить символ - PullRequest
0 голосов
/ 07 марта 2020

Я пытаюсь выучить Grails, перейдя по ссылке: https://guides.grails.org/rest-hibernate/guide/index.html и после создания контроллера ProductController в соответствии с указаниями на странице я получаю следующую ошибку:

Unable to resolve class ProductService in ProductController

Я новичок в groovy и пытаюсь разрешить ссылку на класс путем импорта необходимых пакетов, но ссылка не показывает импорт для разрешения этого класса. в ProductController нет явного оператора импорта для ProductService productService. groovy. Вот классы: 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([]) 
    }
}
}

ProductControllerSpe c:

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

...