JAVA SSM framework redirection and RESTFul framework support

Expansion: Jump

Status code introduction

200: The server processed successfully
404: The resource you were looking for did not exist
500: The server failed to process

redirect

Applicable to the original web page and no longer provide services.
Case:
Introduction: For example, now
JD.com has developed to now. There are many websites. JD Finance and JD Logistics www.jd.com have a login page. Later, there are multiple websites that require unified login. .
jd.com 2003 jd.com/login.html
single sign-on
https://passport.jd.com/new/login.aspx?sso=1&ReturnUrl=https://sso.jdl.cn/sso/redirect
Insert picture description here

Design ideas:

@Controller
  UserController{
    
    
	@RequestMapping("/login"){
    
    
     public String login() {
    
    
     return "redirect:https//路径"
}
}

Create a new UserController.java class

package com.tedu.webDemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {
    
    
     //模拟有个以前的登录方法
	@RequestMapping("/login")
	public String login() {
    
    
		return "redirect:http://www.jd.com";
	}
}

After http://localhost:8080/login is
Insert picture description here
forwarded, the address bar of the browser becomes the forwarded address.
View the status code in the network in the chrome debugging window
Insert picture description here

forward

After forwarding, the browser address bar remains the original address. So the parameters requested before forwarding can still be read after forwarding.
The following example:
Insert picture description here

package com.tedu.webDemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    
    
	@RequestMapping("/loginByNP")
	@ResponseBody //方法返回字符串
	//如果类加的是@RestController 不用加@ResponseBody
	public String loginByNameAndPwd(String username,String pwd) {
    
     //打上断点
		return "loginByNameAndPwd";
	}
	
	@RequestMapping("/loginByC")
	public String loginByCode(String username,String pwd,String code) {
    
    
		//验证码通过
		//调用loginByNameAndPwd
		return "forward:/loginByNP";
	}
	
     //模拟有个以前的登录方法
	@RequestMapping("/login")
	public String login() {
    
    
		return "redirect:http://localhost:8080/login";
	}
}

DeBUG AS execution:
Use breakpoint tracking to find that the username is passed to the loginByNamePwd method.
http://localhost:8080/loginByC?username=123&pwd=456
Insert picture description here

The difference between redirect and forward

The difference between redirection and forwarding
1. The redirect browser address bar displays the new address, and the forwarding address bar remains unchanged
2. Redirection can redirect other websites, and forwarding can only be forwarded to this website.



RESTFul architecture support

RESTFul requires data transmission via url

https://blog.csdn.net/qq_34337272/article/details/108347545
https://www.zhihu.com/question/328810338/answer/720393487

The normal way to pass parameters is url?itemName=phone

There are two ways to pass parameters

普通?username=a&pwd=1
restful /a/1

@restController
    RegisterController{
    
    
@requestMapping("/reg/{username}/{name}")

register@pathVarible String username,@PathVariable String pwd

}

Receive a single parameter

@PathVariable is used to map the template variables in the request URL to the parameters of the function processing method to
form a RUSTFul form, which will be the future development trend. It is more concise and safer. Strengthen the URL GET access method.
Code in the controller:

Create: RegisterController class

package com.tedu.webDemo.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RegisterController {
    
    
	@RequestMapping("/reg/{username}/{pwd}")
	public String register(
			@PathVariable String username,
			@PathVariable String pwd
			) {
    
    
		return username+","+pwd;
	}
}

http://localhost:8080/reg/abc/123
Insert picture description here

Guess you like

Origin blog.csdn.net/QQ1043051018/article/details/112675238