SpringBoot工程添加Web访问

新建demo工程见上一篇
本文将在上一篇的demo工程基础上添加Web访问
新建包controller,在包controller内新建类HelloController,目录如下
在这里插入图片描述HelloController代码如下:

package com.demo.controller;

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

/**
 * Created by cxxlyl on 2020/2/13.
 */
@Controller
public class HelloController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    @ResponseBody  //返回字符串到页面
    public String hello(){
        return "hello";
    }
    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    @ResponseBody
    public String hello1(@RequestParam("name")String name){
        return "hello "+name;
    }
    @RequestMapping(value = "/hello2/{name}",method = RequestMethod.GET)
    @ResponseBody
    public String hello2(@PathVariable("name")String name){
        return "hello "+name;
    }
}

说明:
注解@Controller表明该类为Controller类接收http请求
注解@RequestMapping(value = “/hello”,method = RequestMethod.GET)表示url端口后的路径值为 /hello ,request请求方法为 GET,也可配置为POST或其他的
注解@ResponseBody表示返回数据值,而不是返回值对应的页面

修改配置文件application.properties为application.yml,配置端口为9000
在这里插入图片描述启动项目,在浏览器输入http://localhost:9000/hello,http://localhost:9000/hello1?name=lining,http://localhost:9000/hello/lining,分别得到结果如下
在这里插入图片描述在这里插入图片描述在这里插入图片描述Web访问添加完成,下一篇集成Thymeleaf

发布了3 篇原创文章 · 获赞 0 · 访问量 52

猜你喜欢

转载自blog.csdn.net/qq_31415417/article/details/104288086