如何在Groovy中实现抽象Java类?

时间:2015-10-09 23:44:26

标签: java spring spring-mvc groovy

我正在尝试从Spring's tutorials之一在Groovy中编写一些Spring代码。当我在Groovy中编写以下代码时:

爪哇

@Component
public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

Groovy的

@Component
class SimpleCORSFilter implements Filter {

    void doFilter(ServletRequest req, ServletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse response = res as HttpServletResponse
        response.setHeader("Access-Control-Allow-Origin", "*")
        chain.doFilter(req, res)
    }

    void init (FilterConfig filterConfig) {}
    void destroy() {}
}

我收到以下错误

Can't have an abstract method in a non-abstract class. The class 'com.myproject.main.SimpleCORSFilter' must be declared abstract or the method 'void doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)' must be implemented.
 @ line X, column Y.
   @Component
   ^

问题

我已经实现了抽象的doFilter方法。为什么它无效?

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

这里有语法错误

void doFilter(ServletRequest req, ServletResponse, FilterChain chain)

应该是

void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)