欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot的Controller的使用

程序员文章站 2022-03-14 19:27:56
...
一 Controller相关注解
Spring Boot的Controller的使用
Spring Boot的Controller的使用
二 模板的使用
1 编辑pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd";>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.imooc</groupId>
  <artifactId>girl</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
  </parent>
 
  <properties>
     <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  </dependencies>
 
</project>
2 新建模板文件index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>hello Spring boot</h1>
</body>
</html>
3 HelloController编写
package com.imooc;
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;
@Controller
public class HelloController {
    @Autowired
    private GirlProperties girlProperties;
    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public String say()
    {
        //return girlProperties.getCupSize();
        return "index";
    }
}
4 测试模板
Spring Boot的Controller的使用
三 注解@Controller和@ResponseBody的配合使用
1 HelloController编写
package com.imooc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value="/hello",method=RequestMethod.GET)
     public String say()
     {
           return girlProperties.getCupSize();
     }
}
2 效果测试
Spring Boot的Controller的使用
四 注解@RequestMapping的多URL应用
1 HelloController编写
package com.imooc;
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;
@RestController
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value={"/hello","/hi"},method=RequestMethod.GET)
     public String say()
     {
           return girlProperties.getCupSize();
     }
}
2 效果测试
Spring Boot的Controller的使用
五 注解@RequestMapping修饰整个类的应用
1 HelloController编写
package com.imooc;
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;
@RestController
@RequestMapping("/hello")
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value="/say",method=RequestMethod.GET)
     public String say()
     {
           return girlProperties.getCupSize();
     }
}
2 效果测试
Spring Boot的Controller的使用
六 注解@RequestMapping的POST方式
1 HelloController编写
package com.imooc;
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;
@RestController
@RequestMapping("/hello")
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value="/say",method=RequestMethod.POST)
     public String say()
     {
           return girlProperties.getCupSize();
     }
}
2 postman工具测试效果
Spring Boot的Controller的使用
七 注解@RequestMapping不写POST或GET方式
1 HelloController编写
package com.imooc;
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;
@RestController
@RequestMapping("/hello")
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value="/say")
     public String say()
     {
           return girlProperties.getCupSize();
     }
}
2 postman工具测试效果
Spring Boot的Controller的使用
3 结论
如果注解@RequestMapping不写POST或GET方式,两种方式都可以访问,但为了安全起见,不推荐这样做。

八 注解@PathVariable的应用
1 HelloController编写
package com.imooc;
import org.springframework.beans.factory.annotation.Autowired;
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("/hello")
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value="/say/{id}",method=RequestMethod.GET)
     public String say(@PathVariable("id") Integer id)
     {
           //return girlProperties.getCupSize();
           return "id:"+id;
     }
}
2 测试效果
Spring Boot的Controller的使用
九 注解@RequestParam的基本应用
1 HelloController编写
package com.imooc;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
@RequestMapping("/hello")
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value="/say",method=RequestMethod.GET)
     public String say(@RequestParam("id") Integer myid)
     {
           //return girlProperties.getCupSize();
           return "id:"+myid;
     }
}
2 测试效果
Spring Boot的Controller的使用
十 注解@RequestParam之参数设置默认值应用
1 HelloController编写
package com.imooc;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
@RequestMapping("/hello")
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     @RequestMapping(value="/say",method=RequestMethod.GET)
     public String say(@RequestParam(value="id",required = false,defaultValue="0") Integer myid)
     {
           //return girlProperties.getCupSize();
           return "id:"+myid;
     }
}
2 测试效果
Spring Boot的Controller的使用
十一 注解GetMapping的使用
1 HelloController编写
package com.imooc;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
@RequestMapping("/hello")
public class HelloController {
     @Autowired
     private GirlProperties girlProperties;
     
     //@RequestMapping(value="/say",method=RequestMethod.GET)
     @GetMapping(value="/say")
     public String say(@RequestParam(value="id",required = false,defaultValue="0") Integer myid)
     {
           //return girlProperties.getCupSize();
           return "id:"+myid;
     }
}
2 测试效果
Spring Boot的Controller的使用
相关标签: 注解