spring boot 入门(三)controller的使用

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(15113)   2023-03-28 11:29:14

一.常用注解说明

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

二.@RestController 使用

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;
	}
}

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

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

如上面代码中的一样,可以作用于方法上,但是也可以作用于类上,作用于类上就相当于给所有的方法添加了一个前缀
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;
	}
}
上面代码的访问地址为:
fw

四.@PathVariable注解使用

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;
	}
}
注意观察,注解@PathVariable使用需要在url中添加一个大括号对,中间是名字,方法参数里面@PathVariable("name"),中间的name必须与url的一致
写好后,重启
浏览器输入地址:
浏览
地址说明:最后的xqlee为传递的参数

五.@RequestParam注解使用

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;
	}
}
访问地址:
fw
注意:
1.访问地址中的参数名name一定要和@RequestParam("name")注解中的name一致,后面的参数myName可以不与前面一致

六.@GetMapping注解使用

注解@GetMapping,主要是简化@RequestMapping,@GetMapping等同于@RequestMapping设置method=RequestMethod.GET
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;
	}
}
改后同样可以访问,当然同理还有其他方式的mapping

推荐:
spring boot 入门(一)hello word
spring boot入门(二)属性配置
spring boot入门(三)controller的使用
spring boot入门(四)数据库操作
地址:https://www.leftso.com/article/66.html

相关阅读

spring boot又一个spring框架的经典项目,本文讲解spring boot入门的环境配置以及第一个项目,Spring Boot 入门教程
spring boot入门,spring boot是一个崭新的spring框架分支项目,本文讲解其属性配置相关
spring boot是一个崭新的spring框架分支项目,本文讲解基本的数据库配置
spring boot是一个崭新的spring框架分支项目,本文讲解spring boot中controller的常用注解使用
Spring Boot 入门 AOP 日志处理,这里主要讲解spring boot 中采用AOP方式进行日志的统一处理。spring 框架的处理也是一样。综合来讲,本文主要讲解了spring b...
Spring boot 入门之CORS 跨域配置详解,spring 跨域配置详解。
spring boot 入门 使用spring.profiles.active来分区配置,,在spring boot中可以存在多个环境的配置文件通过配置spring.profiles.activ...
spring boot 1.5整合redis实现spring的缓存框架,spring boot,redis
spring boot 入门之整合spring session实现session共享。一直以来Java编程中web项目中的session共享问题都是一个很难解决的问题。接下来将讲解通过sprin...
spring boot mybatis 整合使用讲解介绍,spring boot与mybaties的使用讲解介绍。spring boot mybatis xml mapper方式的入门和通过一个...