Simple Routing Interface

Create a familiar

style
[TestController.java]

package com.demo;

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

@RestController
public class TestController {
	@RequestMapping(value = "/print")
	public String print(){
		return "Hello Controller!";
	}
}


Browser access is correct http://localhost:8080/print
------------------------------------- -------------------------------
GET-URL with parameter mode 1
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@RequestMapping(value = "/{name}/print/" , method = RequestMethod.GET)
	public String print(@PathVariable("name") String name){
		return "WelCome:"+name;
	}
}


Browser test: http://localhost:8080/s/print/

POST-with parameters
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@RequestMapping(value = "/print/{name}" , method = RequestMethod.POST)
	public String print(@PathVariable("name") String name){
		return "WelCome:"+name;
	}
}


Browser test: http://localhost:8080/print/s



Other ways to obtain Request Scope Parameter

import org.springframework.web.bind.annotation.RequestParam;
@RestController
public class TestController {
	@RequestMapping(value = "/print" , method = RequestMethod.GET)
	public String print(@RequestParam("name") String name){
		return "WelCome:"+name;
	}
}


Test: http://localhost:8080/print?name=123


Redirect jump page mode

resources/ directory
Create /templates/ and

put the page to be jumped hello.html


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

@Controller
@ResponseBody
public class TestController {
	@RequestMapping(value = "/print" , method = RequestMethod.GET)
	public String print(){
		return "hello";
	}
}


Test can jump





, different routes access the same return

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class TestController {
	@RequestMapping(value = {"/print","/hello"} , method = RequestMethod.GET)
	public String print(){
		return "hello";
	}
}








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326335098&siteId=291194637