Spring boot中Controller的使用

一、请求及路径映射部分注解介绍

注解名称 描述
@Controller 处理http请求
@RestController Spring4之后新加的注解,原来返回json,需要@ResponseBody配合@Controller
@RequestMapping 配置url映射

 

 

 

 

 

1、@Controller的使用(了解)

相当于serverlet的jsp,前后端不分离的开发,就模板而言很好性能,所以提倡前后端分离式的开发,这里我就不啰嗦了。

接着再来演示下,这个注解的使用,必须配合模板来使用,首先在pom文件中添加如下依赖:

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>

说明:这里的spring boot版本必须为1.4.1,否则使用模板时,不显示页面,报错404

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

在src/main/resources/templates/下,

创建一个名为index的html文件,写入内容如下:

<h1>hello,Spring boot!</h1>

编写Controller,示例如下:

复制代码
package com.rongrong.springboot.demo;

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

/**
 * @author rongrong
 * @version 1.0
 * @description:
 * @date 2019/12/28 21:32
 */
@Controller
public class HtmlController {

    @RequestMapping(value = "/say",method = RequestMethod.GET)
    public String index(){
        return "index";
    }
}
复制代码

启动项目,访问页面:http://localhost:8888/say,显示结果如下:

 

 

 2、@RestController的使用

@RestController在实际开发中应用广泛,与@controller相比,不依赖于模板,前后端分离式的开发,开发效率也很高。

3、@RequestMapping的使用

该注解既可以在类上方使用,又可以方法上使用,在类上方使用,如在该处添加,接口访问时采取路径拼接方式访问,如:localhost:8888/spring/hellow

示例代码如下:

复制代码
package com.rongrong.springboot.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author rongrong
 * @version 1.0
 * @description:
 * @date 2019/12/26 20:34
 */
@RestController
/**
 * 在类上方使用,如在该处添加,接口访问时采取路径拼接方式访问
 * 如:localhost:8888/spring/hellow
 */
@RequestMapping("/spring")
public class HellowController {


    @Autowired
    Person person;
    //在方法 上方使用
    @RequestMapping(value = "/hellow",method = RequestMethod.GET)
    public String say(){
        return person.getGName();
    }

}
复制代码

访问接口地址:http://localhost:8888/spring/hellow,具体效果如下:

 

 

 

二、请求参数及组合注解介绍

注解名称 描述
@PathVariable 获取url中的数据
@RequestParam  获取请求参数的值
@GetMapping 组合注解

 

 

 

 

1、@PathVariable的使用

一般在restful风格接口使用居多,形式为:/路径/{变量}

具体示例如下:

    //Restful风格接口
    @RequestMapping(value = "/getID/{id}", method = RequestMethod.GET)
    public String getID(@PathVariable("id") Integer id) {
        return "Id:"+id;
    }

访问示例:http://localhost:8888/spring/getID/1,效果如下:

 

 

 2、@RequestParam使用

 为传统风格形式使用,具体示例如下:

复制代码
   /***
     * 传统方式接口
     * @RequestParam(value = "id",required = false,defaultValue = "0")
     * value为传入字段名,required为true时必须传参,defaultValue为传入参数默认值
     * @param id
     * @return
     */
    @RequestMapping(value = "/getMyId", method = RequestMethod.GET)
    public String getMyId(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id) {
        return "getMyId: \t"+id;
    }
复制代码

访问示例:http://localhost:8888/spring/getMyId?id=1,效果如下:

 

 

 3、组合注解及多个路径可以访问同一个接口

@GetMapping可以理解为@RequestMapping,具体示例如下:

复制代码
 /**
     * 组合注解及多个路径可以访问同一个接口
     * @param id
     * @return
     */
    @GetMapping({"/getUserId","/sayhi"})
    //@RequestMapping(value = "/getMyId", method = RequestMethod.GET)
    public String getUserId(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id) {
        return "getMyId: \t"+id;
    }
复制代码

访问示例:

http://localhost:8888/spring/getUserId?id=1

http://localhost:8888/spring/sayhi?id=1

效果如下:

 

 

 

posted @   久曲健  阅读(17169)  评论(1编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2016-12-29 appium如何获取conten-desc内容文本
点击右上角即可分享
微信分享提示