spring boot是一个崭新的spring框架分支项目,本文讲解spring boot中controller的常用注解使用<h4>一.常用注解说明</h4>
@Controller 处理http请求<br />
@RestController Spring框架4版本之后出来的注解,之前版本返回json数据需要@ResponseBody配合@Controller<br />
@RequestMapping 配置url映射关系<br />
@PathVariable 获取url中的数据<br />
@RequestParam 获取请求参数的值<br />
@GetMapping 组合注解
<h4>二.@RestController 使用</h4>
<pre>
<code class="language-java">package com.example;
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;
/**
* create leftso.com
*/
@RestController
public class HelloController {
@Value("${cupSize}")//注意写法,获取配置文件中的cupSize
private String cpuSize;
@Value("${age}")
private int age;
@Value("${content}")
private String content;
@Autowired
PersionProperties persionProperties;
@RequestMapping(value="/hello",method=RequestMethod.GET)//写法与springMVC有点相似
public String say(){
System.out.println("name:"+persionProperties.getName()+" age:"+persionProperties.getAge());
return "Hello Spring Boot!"+cpuSize+" "+age+" "+content;
}
}
</code></pre>
<br />
注解@RestController标记在类上,表示该类处理http请求,并且返回json数据
<h4>三.@RequestMapping注解使用(与SpringMVC中的使用相同)</h4>
如上面代码中的一样,可以作用于方法上,但是也可以作用于类上,作用于类上就相当于给所有的方法添加了一个前缀
<pre>
<code class="language-java">package com.example;
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;
/**
* create leftso.com
*/
@RestController
@RequestMapping("/demo")
public class HelloController {
@Value("${cupSize}")//注意写法,获取配置文件中的cupSize
private String cpuSize;
@Value("${age}")
private int age;
@Value("${content}")
private String content;
@Autowired
PersionProperties persionProperties;
@RequestMapping(value="/hello",method=RequestMethod.GET)//写法与springMVC有点相似
public String say(){
System.out.println("name:"+persionProperties.getName()+" age:"+persionProperties.getAge());
return "Hello Spring Boot!"+cpuSize+" "+age+" "+content;
}
}
</code></pre>
上面代码的访问地址为:<br />
<img alt="fw" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-02/b802991b-dc1b-42fb-bbf1-5fb777e3e09e.jpg" style="height:256px; width:649px" />
<h4>四.@PathVariable注解使用</h4>
<pre>
<code class="language-java">package com.example;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* create leftso.com
*/
@RestController
@RequestMapping("/demo")
public class HelloController {
@Value("${cupSize}")//注意写法,获取配置文件中的cupSize
private String cpuSize;
@Value("${age}")
private int age;
@Value("${content}")
private String content;
@Autowired
PersionProperties persionProperties;
@RequestMapping(value="/hello/{name}",method=RequestMethod.GET)//这里也可以写成/{name}/hello
public String say(@PathVariable("name") String name){
return "Hello :"+name;
}
}
</code></pre>
注意观察,注解@PathVariable使用需要在url中添加一个大括号对,中间是名字,方法参数里面@PathVariable("name"),中间的name必须与url的一致<br />
写好后,重启<br />
浏览器输入地址:<br />
<img alt="浏览" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-02/c6469e03-0d70-4027-be66-8f4d957bbbcd.jpg" style="height:343px; width:730px" /><br />
地址说明:最后的xqlee为传递的参数
<h4>五.@RequestParam注解使用</h4>
<pre>
<code class="language-java">package com.example;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* create leftso.com
*/
@RestController
@RequestMapping("/demo")
public class HelloController {
@Value("${cupSize}")//注意写法,获取配置文件中的cupSize
private String cpuSize;
@Value("${age}")
private int age;
@Value("${content}")
private String content;
@Autowired
PersionProperties persionProperties;
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String say(@RequestParam("name") String myName){
return "Hello :"+myName;
}
}
</code></pre>
访问地址:<br />
<img alt="fw" class="img-thumbnail" src="/assets/upload/blog/thumbnail/2017-02/d77c2ff6-ec05-4432-9115-37b51742cece.jpg" style="height:218px; width:584px" /><br />
注意:<br />
1.访问地址中的参数名name一定要和@RequestParam("name")注解中的name一致,后面的参数myName可以不与前面一致
<h4>六.@GetMapping注解使用</h4>
注解@GetMapping,主要是简化@RequestMapping,@GetMapping等同于@RequestMapping设置method=RequestMethod.GET
<pre>
<code class="language-java">package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* create leftso.com
*/
@RestController
@RequestMapping("/demo")
public class HelloController {
@Value("${cupSize}")//注意写法,获取配置文件中的cupSize
private String cpuSize;
@Value("${age}")
private int age;
@Value("${content}")
private String content;
@Autowired
PersionProperties persionProperties;
// @RequestMapping(value="/hello",method=RequestMethod.GET)
@GetMapping(value="/hello")
public String say(@RequestParam("name") String myName){
return "Hello :"+myName;
}
}
</code></pre>
改后同样可以访问,当然同理还有其他方式的mapping<br />
<br />
推荐:<br />
<a rel="" target="_blank"href="http://www.leftso.com/blog/64.html" rel="" target="_blank">spring boot 入门(一)hello word</a><br />
<a rel="" target="_blank"href="http://www.leftso.com/blog/65.html" rel="" target="_blank">spring boot入门(二)属性配置</a><br />
<a rel="" target="_blank"href="http://www.leftso.com/blog/66.html" rel="" target="_blank">spring boot入门(三)controller的使用</a><br />
<a rel="" target="_blank"href="http://www.leftso.com/blog/67.html" rel="" target="_blank">spring boot入门(四)数据库操作</a>