springBoot 入门(三)—— 使用 RestController

一.常用注解说明 https://blog.csdn.net/uniquewonderq/article/details/79971433?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-6.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-6.channel_param

  • @Controller 处理http请求
  • @RestController Spring框架4版本之后出来的注解,之前版本返回json数据需要@ResponseBody配合@Controller
  • @RequestMapping 配置url映射关系
  • @PathVariable 获取url中的数据
  • @RequestParam 获取请求参数的值
  • @GetMapping 组合注解

二.@RestController 使用

package HelloWord;

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

@RestController
public class HelloController {

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${cupSize}"</span>)<span class="hljs-comment">//注意写法,获取配置文件中的cupSize</span>
<span class="hljs-keyword">private</span> String cpuSize;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${age}"</span>)
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${content}"</span>)
<span class="hljs-keyword">private</span> String content; 


<span class="hljs-annotation">@RequestMapping</span>(value=<span class="hljs-string">"/hello"</span>,method= RequestMethod.GET)<span class="hljs-comment">//写法与springMVC有点相似</span>
<span class="hljs-keyword">public</span> String <span class="hljs-title">say</span>(){
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello Spring Boot!"</span>+cpuSize+<span class="hljs-string">" "</span>+age+<span class="hljs-string">" "</span>+content;
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

application-prd.properties文件:

#模拟生产变量
curvar=prd.curvar

cupSize = 16g

age = 24

content = HelloWord-prd

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

启动spring boot后,在浏览器中输入:
localhost:8889/hello

这里写图片描述

注解@RestController标记在类上,表示该类处理http请求,并且返回json数据

三.@RequestMapping注解使用(与SpringMVC中的使用相同)

如上面代码中的一样,可以作用于方法上,但是也可以作用于类上,作用于类上就相当于给所有的方法添加了一个前缀。再次访问之前的会报错

这里写图片描述

加上前缀后,则可以访问

这里写图片描述

四.@PathVariable注解使用

package HelloWord;

import org.springframework.beans.factory.annotation.Value;
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
@RequestMapping("/demo")
public class HelloController {

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${cupSize}"</span>)<span class="hljs-comment">//注意写法,获取配置文件中的cupSize</span>
<span class="hljs-keyword">private</span> String cpuSize;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${age}"</span>)
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${content}"</span>)
<span class="hljs-keyword">private</span> String content;


<span class="hljs-annotation">@RequestMapping</span>(value=<span class="hljs-string">"/hello/{name}"</span>,method=RequestMethod.GET)<span class="hljs-comment">//这里也可以写成/{name}/hello</span>
<span class="hljs-keyword">public</span> String <span class="hljs-title">say</span>(@<span class="hljs-title">PathVariable</span>("name") String name){
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello :"</span>+name;
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

注意观察,注解@PathVariable使用需要在url中添加一个大括号对,中间是名字,方法参数里面@PathVariable(“name”),中间的name必须与url的一致
写好后,重启

在浏览器中:

这里写图片描述
地址说明:最后的zhang为传递的参数

五.@RequestParam注解使用

package HelloWord;
    <span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;
    <span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/demo")
public class HelloController {

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${cupSize}"</span>)<span class="hljs-comment">//注意写法,获取配置文件中的cupSize</span>
<span class="hljs-keyword">private</span> String cpuSize;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${age}"</span>)
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${content}"</span>)
<span class="hljs-keyword">private</span> String content;


<span class="hljs-annotation">@RequestMapping</span>(value=<span class="hljs-string">"/hello"</span>,method=RequestMethod.GET)
<span class="hljs-keyword">public</span> String <span class="hljs-title">say</span>(@<span class="hljs-title">RequestParam</span>("name") String myName){
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello :"</span>+myName;
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

访问地址
这里写图片描述

访问地址中的参数名name一定要和@RequestParam(“name”)注解中的name一致,后面的参数myName可以不与前面一致

六.@GetMapping注解使用

注解@GetMapping,主要是简化@RequestMapping,@GetMapping等同于@RequestMapping设置method=RequestMethod.GET

package HelloWord;
    <span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;
    <span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/demo")
public class HelloController {

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${cupSize}"</span>)<span class="hljs-comment">//注意写法,获取配置文件中的cupSize</span>
<span class="hljs-keyword">private</span> String cpuSize;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${age}"</span>)
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;

<span class="hljs-annotation">@Value</span>(<span class="hljs-string">"${content}"</span>)
<span class="hljs-keyword">private</span> String content;

// @RequestMapping(value="/hello",method=RequestMethod.GET)
@GetMapping(value="/hello")
public String say(@RequestParam("name") String myName){
return "Hello :"+myName;
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

改后重启,同样可以访问,当然同理还有其他方式的mapping

这里写图片描述

posted @ 2020-10-22 09:26  百丈山居客  阅读(949)  评论(0)    收藏  举报