我有一个用@WebServlet("*.html")
注释的servlet和一个用@GetMapping("/greeting.html")
注释的控制器。尽管控制器映射更具体,但servlet优先。
在我的应用程序中,我无法轻易更改servlet的映射(我正处于从servlet到Spring-Boot的复杂迁移之中)。
我尝试了@Order
注释和其他匹配规则。到目前为止没有任何进展。
您可以仅使用3个类来重现它:
application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
GreetingController.java
package hello;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class GreetingController {
@GetMapping("/greeting.html")
@Order(Ordered.HIGHEST_PRECEDENCE)
@ResponseBody
public String greeting() {
return "this is geeting.html";
}
}
SimpleServlet.java
package hello;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@WebServlet("/*.html")
@Order(Ordered.LOWEST_PRECEDENCE)
public class SimpleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("This is *.html servlet");
super.doGet(req, resp);
}
}
访问localhost:8080
时,我希望看到this is geeting.html
,反而得到This is *.html servlet
。