gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:04:59 24 4
gpt4 key购买 nike

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

Java

@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方法。为什么无效?

我该如何解决这个问题?

最佳答案

这里有语法错误

void doFilter(ServletRequest req, ServletResponse, FilterChain chain)

应该是

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

关于java - 如何在 Groovy 中实现抽象 Java 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com