spring boot入门(二) 简单web开发helloworld版本

1、创建一个HelloController,用于在页面上打印“hello spring boot”的话,如下

spring boot入门(二) 简单web开发helloworld版本
代码如下:

package com.example.demo.controller;

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

/**
 * 返回json串的用@RestController,返回网页的用@Controller
 * @author huangqh
 *
 */
@RestController
public class HelloController {
	@RequestMapping("/hello")
	public String hello() {
		String msg = "hello spring boot";
		return msg;
	}
}
2、启动spring boot,在浏览器上输入http://localhost:8080/hello,弹出需要登录并且网址跳转到http://localhost:8080/login,如下

spring boot入门(二) 简单web开发helloworld版本
原因: 因为项目引用了spring security,不需要的可以去掉,默认用户是user,密码是程序启动时自动生成的一串字符串,如下:
spring boot入门(二) 简单web开发helloworld版本
修改默认的用户名密码: 在application.properties中添加以下代码

#设置用户名
spring.security.user.name=admin
#设置密码
spring.security.user.password=admin

重启后输入用户和密码后,显示以下界面打印了“hello spring boot”
spring boot入门(二) 简单web开发helloworld版本

3、接收参数的hello实现,代码如下
public class HelloController {
	@RequestMapping("/hello")
	public String hello(String name) {
		String msg = "hello " + name;
		return msg;
	}
}
4、启动spring boot,在浏览器上输入http://localhost:8080/hello?name=myspringboot,如下

spring boot入门(二) 简单web开发helloworld版本