Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does redirect mean in SpringBoot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the meaning of redirection in SpringBoot". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

i. Environment building

First of all, you have to build a web application before it is possible to continue the follow-up testing. Building a web application with SpringBoot is a relatively simple task.

Create a maven project with the following pom file

Org.springframework.boot spring-boot-starter-parent 2.1.7 UTF-8 UTF-8 Finchley.RELEASE 1.8 org.springframework.boot spring-boot-starter-web com.alibaba fastjson 1.2.45 org.springframework.boot spring-boot-maven-plugin Spring-milestones Spring Milestones https://repo.spring.io/milestone false

It's still a normal process. After the pom dependency is done, write a program entry.

/ * Created by @ author yihui in 15:26 19-9-13. * / @ SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class);}} II .302 Redirect 1. Return to redirect

This kind of case is usually suitable for the interface that returns the view, adding redirect: to the returned string to tell the Spring framework that 302 redirection is required.

@ Controller@RequestMapping (path = "redirect") public class RedirectRest {@ ResponseBody @ GetMapping (path = "index") public String index (HttpServletRequest request) {return "redirect access!" + JSON.toJSONString (request.getParameterMap ());} @ GetMapping (path = "R1") public String R1 () {return "redirect:/redirect/index?base=r1";}}

A simple demo is given above. When we visit / redirect/r1, we will redirect to the request / redirect/index?base=r1. The actual test results are as follows

Notice the screenshot above. The connection we actually visit is http://127.0.0.1:8080/redirect/index?base=r1, which shows that the request url becomes http://127.0.0.1:8080/redirect/index?base=r1 in the browser. The status code checked by the console is 302.

Description

The premise of using this method is that the @ ResponseBody annotation cannot be added to the interface, otherwise the returned string will be treated as an ordinary string and will not be redirected.

2. HttpServletResponse redirection

In the previous article, when we talked about SpringMVC returning data, we introduced the way that we can write data directly to the output stream through HttpServletResponse to return the result. We also use it here to achieve redirection.

@ ResponseBody@GetMapping (path = "R2") public void R2 (HttpServletResponse response) throws IOException {response.sendRedirect ("/ redirect/index?base=r2");}

From the demo above, you can also see that the way to use this is very simple. Just call javax.servlet.http.HttpServletResponse#sendRedirect and pass in the url that needs to be redirected.

3. Summary

This paper mainly introduces two common back-end redirection methods, both of which are relatively simple, and these two methods also have their own applicable scenarios (not absolute, of course).

Adding redirect in front of the return view is more suitable for view jump, jumping from one page to another.

The HttpServletResponse#sendRedirec approach is more flexible and can be used at any stage in the life cycle of receiving an http request at the back end, such as the following common scenarios

When an interface requires login, redirect to the login page at the interceptor layer for all requests that are not logged in

In global exception handling, if a server exception occurs, redirect to the customized 500 page

Unsupported request, redirect to 404 page

This is the end of the content of "what is the meaning of redirection in SpringBoot"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report