如何在Grails中正确使用休息服务

时间:2019-05-18 21:40:02

标签: rest grails spring-security

我正在尝试使用脚手架和rest api创建grails应用程序,使用脚手架一切正常,但是当我尝试从其余api获取信息时会遇到一些错误 我的代码:

package estudiolegal

import grails.plugin.springsecurity.annotation.Secured
import grails.rest.Resource

import static org.springframework.http.HttpStatus.*

@Resource(uri='/api/proyectos', formats=['json', 'xml'])
@Secured(['ROLE_ADMIN','ROLE_ABOGADO','ROLE_CLIENTE'])
class ProyectoController {
    ProyectoService proyectoService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def index = {
        redirect action: "list"
    }

    def create() {
        respond new Proyecto(params)
    }

    def save = {
        def proyecto = new Proyecto(params)
        proyecto.addToUsuarios(applicationContext.springSecurityService.currentUser)
        proyecto.save flush: true, failOnError: true
        redirect action: "show", id: proyecto.id
    }

    def edit(Long id) {
        respond proyectoService.get(id)
    }

    def update = {
        def proyecto = Proyecto.get(params.id)
        proyecto.properties = params
        proyecto.save flush: true, failOnError: true
        redirect action: "show", id: params.id
    }

    def show (Long id){
        respond proyectoService.get(id)
    }

    def list = {
        def proyectos = Proyecto.list()
        [proyectos: proyectos]
    }

    def delete = {

        def proyecto = Proyecto.get(params.id)
        proyecto.usuarios.each {
            if(it.proyectos.contains(proyecto))
            {
                it.removeFromProyectos(proyecto)
            }
        }
        proyecto.delete flush: true, failOnError: true
        redirect action: "index"
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'proceso.label', default: 'Proyecto'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }

}

当我尝试触发此http://localhost:8080/api/proyectos,而RestClient失败时会显示错误

2019-05-18 15:36:28.202错误--- [nio-8080-exec-8] o.g.web.errors.GrailsExceptionResolver:处理请求时发生MissingMethodException:[GET] / api / proyectos 没有方法签名:static estudiolegal.ProyectoController.count()适用于参数类型:()值:[] 可能的解决方案:print(java.io.PrintWriter),print(java.lang.Object),wait(),create(),edit(),list()。 Stacktrace如下:

java.lang.reflect.InvocationTargetException:空     在org.grails.core.DefaultGrailsControllerClass $ ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)     在org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)     在org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)     在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     在grails.plugin.springsecurity.rest.RestLogoutFilter.doFilter(RestLogoutFilter.groovy:80)     在org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:317)     在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)     在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在grails.plugin.springsecurity.web.filter.GrailsHttpPutFormContentFilter.doFilterInternal(GrailsHttpPutFormContentFilter.groovy:54)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在grails.plugin.springsecurity.rest.RestTokenValidationFilter.processFilterChain(RestTokenValidationFilter.groovy:118)     在grails.plugin.springsecurity.rest.RestTokenValidationFilter.doFilter(RestTokenValidationFilter.groovy:84)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:54)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在grails.plugin.springsecurity.rest.RestAuthenticationFilter.doFilter(RestAuthenticationFilter.groovy:143)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:64)处     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)     在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:331)     在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)     在org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)     在org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)     在org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在java.lang.Thread.run(Thread.java:748) 原因:groovy.lang.MissingMethodException:方法的无签名:static estudiolegal.ProyectoController.count()适用于参数类型:()值:[] 可能的解决方案:print(java.io.PrintWriter),print(java.lang.Object),wait(),create(),edit(),list()     在grails.rest.RestfulController.countResources(RestfulController.groovy:276)     在grails.rest.RestfulController.index(RestfulController.groovy:62)     在grails.gorm.transactions.GrailsTransactionTemplate $ 2.doInTransaction(GrailsTransactionTemplate.groovy:94)     在org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)     在grails.gorm.transactions.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:91)     ...省略了38个常见框架

0 个答案:

没有答案