2. 基本请求controller的创建

(在上一节中创建好spring boot以后,就可以开始创建controller了,用于处理web请求)

1. 在spring boot的程序入口所在的 .java文件的同级目录,创建一个 controller.java文件

2. controller中代码如下

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

@Controller
public class Controller {
	@RequestMapping("/index")
	@ResponseBody
	public String index() {
		return "<h3>this is index page</h3>";
	}
}

然后,运行spring boot ,打开 localhost:8080/index

就能看到

this is index page

这样,一个简单的请求以及返回就完成了。

原文地址:https://www.cnblogs.com/hcy1994/p/7784843.html