如何处理Java Spring中的所有其他地址

发布于 2025-02-07 18:27:34 字数 304 浏览 2 评论 0原文

我想处理我没有制作特定功能的所有地址。

例如,

@GetMapping({"/"}) // handles "/"
public String main(Model model) {
    return "homepage";
}
@GetMapping({"/admin"}) // handles "/admin"
public String main(Model model) {
    return "admin";
}     

如何处理所有其他地址,例如“/asdf”等,并将它们全部发送到特定页面?

I want to handle all the address which I didn't make a specific function for.

For example,

@GetMapping({"/"}) // handles "/"
public String main(Model model) {
    return "homepage";
}
@GetMapping({"/admin"}) // handles "/admin"
public String main(Model model) {
    return "admin";
}     

How can I handle all the other address, such as "/asdf" etc and send them all to a specific page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情栀口红 2025-02-14 18:27:34

这样的事情可以帮助您:

@GetMapping("/{whatever}")
public void xxx(@PathVariable String whatever) {
    // Do things
}

Something like this could help you:

@GetMapping("/{whatever}")
public void xxx(@PathVariable String whatever) {
    // Do things
}
我乃一代侩神 2025-02-14 18:27:34
@RequestMapping(value="*", method = RequestMethod.GET)
public String fallback(){
return "specific-page";
}

只要确保这是您的最后一个请求图,我记得在Node.js中,它具有奇怪的结果,直到将其放置在同类的底部,因此要安全起见,但是它在Java类中也放在底部。

@RequestMapping(value="*", method = RequestMethod.GET)
public String fallback(){
return "specific-page";
}

Just make sure that this is your last RequestMapping, I remember that in node.js it has weird results until it was placed on the bottom of the class, so just to be safe, but it on the bottom as well in your java class.

谈下烟灰 2025-02-14 18:27:34

您可以使用 @路径范围为此。

@GetMapping("/{var}") // handles any string you can pass
public String main(@PathVariable(value="var",required=true)String var) {
    return "anyString";
} 

You can use @PathVariable for this.

@GetMapping("/{var}") // handles any string you can pass
public String main(@PathVariable(value="var",required=true)String var) {
    return "anyString";
} 
初与友歌 2025-02-14 18:27:34

另一个选项是编写自定义处理程序,在此处您将在此处使用逻辑,该逻辑将在用户尝试进入不存在的路径时执行。

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.logging.Level;
import java.util.logging.Logger;

@RestControllerAdvice
public class ExceptionHandlerAOP {


    @ExceptionHandler(Exception.class)
    public ModelAndView handleError(HttpServletRequest request, Exception e)   {
        // when some other exception occures, you can handle it here..
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ModelAndView handleError404(HttpServletRequest request, Exception e)   {
        //do whatever you want, return some page..
    }


}

Another option is to write custom exception handler, where you will place logic that will execute when user tries to go to path that doesn't exist.

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.logging.Level;
import java.util.logging.Logger;

@RestControllerAdvice
public class ExceptionHandlerAOP {


    @ExceptionHandler(Exception.class)
    public ModelAndView handleError(HttpServletRequest request, Exception e)   {
        // when some other exception occures, you can handle it here..
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ModelAndView handleError404(HttpServletRequest request, Exception e)   {
        //do whatever you want, return some page..
    }


}
流殇 2025-02-14 18:27:34

是的,您可以使用这样的通配符

@GetMapping("/**")
public String handle() {
    return handleException(exception, "404");
}

,但我认为将其视为404错误更加优雅,然后您可以按照自己的意愿继续

 @ExceptionHandler(NoHandlerFoundException.class){
  public ModelAndView handlePageNotFoundException(final Exception exception) {
    return handleException(exception, "404");
  }

Yes, you can use wildcards like this

@GetMapping("/**")
public String handle() {
    return handleException(exception, "404");
}

but I think it is more elegant to catch it as a 404 error and then you can continue as you like

 @ExceptionHandler(NoHandlerFoundException.class){
  public ModelAndView handlePageNotFoundException(final Exception exception) {
    return handleException(exception, "404");
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文