Springboot注解

maven中setting.xml中添加一个,下载依赖包比较快。

<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        

    </mirror>

springmvc、springboot ,都是在DispatherServlet基础上封装的,底层都是servlet。

注解类型,@GetMapping @PostMapping @RequestMapping(即支持get、有支持post,可以用method指定),这三个都用在方法上。@RequestMapping,还可以用来修饰类,相当多了一个层级。

@RequestMapping(value = "/a",method = RequestMethod.get,method = RequestMethod.post)


@RestController 只是用于接口 默认返回数据格式为json,位置用在类上面。因为内置了返回类型为json,Map map,User user,List list.

@Controller + @ResponseBody 组合等同于 @RestController ,单独使用@Controller 返回的是视图,jsp, 位置用在类上面。

 

参数中必须包含username,age

@RequestMapping(value = "/cc",method = RequestMethod.get, params = {"username","age!=10"},headers = {"Host:127.0.0.1:8080"},)



热部署在运行中重新加载编译后的class,springboot,里面1、依赖了一个spring-boot-devtools,

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>true</scope>
</dependency>

 2、配一个插件true,

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <!-- 没有该配置,devtools 不生效 -->
            <fork>true</fork>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

还有一步操作,把东西从新编译一下,然后热部署会把它从新加载到内存里面,从新编译用到idea里面 build project,就会重新编译。ctrl+f9 快捷键 热部署。一个类的时候的挺快,在很多类的时候也慢。

value ,required ,defaultValue 三个的使用

HttpServletRequest 

获取参数 @RequestParam(defaultValue = "sds")String userName,defaultValue 如果没有传入userName的值,默认会取userName = sds 

@RequestParam(value = "name",defaultValue = "")String userName,传的是name的属性,传到了userName的字符串上面,这样的好处是,安全
url:http:127.0.0.1:8080/aa?name=sdf&age=3  url上name 参数值映射到userName上
@RequestParam()String userName,@RequestParam()String age
url:http:127.0.0.1:8080/aa?userName=32&age=32

@RequestParam(required = flase)String age  age参数可以不用传。当里面不设置任何属性的时候,请求的url参数必须包含age, 如果required = false。当有默认值的时候,即使required设置为true 也可以请求,去默认值里的值。

传json格式 并且用post方法
@RequestMapping(value = "/requestBodyTest")
publlic @ResponseBody
Map requestBodyTest(@RequestBody Map body,HttpServletRequest request){
 Map map = new HasMap();
 List list = new ArrayList();
map.put ("niu","name");
list.add(map);
return list;
}


面向资源请求
@ResquestMapping(value = "/{name}/{email}")
public @ResponseBody
String restfulTest(@PathVariable(vallue = "/ss") String name ,@PathVariable String emali){
 return name+":"+email;


}

@GetMapping 定义get请求,位置只能用于方法上面

@PostMpaping 定义为post请求,位置只能用于方法上面

@RequestMapping 既可以是get 也可以是post ,位置位于方法上面,还可以用于类上面,相当多加了一层路径。

@RequestParam 用于方法参数中

restful 风格的接口,

用@PathVariable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值