3.注解

作者: 0f701952a44b | 来源:发表于2020-03-19 09:14 被阅读0次
1.@RestController =@Controller+@ResponseBody

@ResponseBody表示将返回结果使用json数据返回前端

2.@SpringBootApplication=@Configuration+@EnableAutoConfiguration+@ComponentScan
3.

@GetMapping=@RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
@PathVariable(获取路径中的值)
@RequestParam(设置传入参数默认值,例如分页)
@RequestBody(要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
请求时需要指定http头为 content-type为application/json charset=utf-8)
@RequestHeader(获取http头信息,比如鉴权)

@RestController
public class GetRequestTestController {
    Map<String,Object> map = new HashMap<String,Object>();
    /**
     * 测试restful协议,从路径中获取协议,测试URL:http://localhost:8080/1/12
     * @param cityId
     * @param userId
     * @return
     */
    @RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET)
    public Object findUser(@PathVariable("city_id")String cityId,@PathVariable("user_id")String userId) {
        map.clear();
        map.put("cityid", cityId);
        map.put("userid", userId);
        return map;
    }
    /**
     * 测试@GetMapping(@GetMapping是@RequestMapping(method = RequestMethod.GET)的缩写),测试URL:http://localhost:8080/v1/test?city_id=1&user_id=12
     * 返回结果:{"cityid":"1","userid":"12"} 
     * @param city_id
     * @param user_id
     * @return
     */
    @GetMapping(value = "/v1/test")
    public Object findUser2(String city_id,String user_id) {
        map.clear();
        map.put("cityid", city_id);
        map.put("userid", user_id);
        return map;
    }
    /**
     * 测试@GetMapping、@PathVariable其中@PathVariable获取路径中的值,测试URL:http://localhost:8080/v1/test/1?city_id=1&user_id=12
     * 返回结果:{"id":"1","cityid":"1","userid":"12"}
     * @param id
     * @param city_id
     * @param user_id
     * @return
     */
    @GetMapping(value="/v1/test/{id}")
    public Object findUser3(@PathVariable("id")String id,String city_id,String user_id) {
        map.clear();
        map.put("id", id);
        map.put("cityid", city_id);
        map.put("userid", user_id);
        return map;
    }
    /**
     * 测试@RequestParam 传入参数默认值,是否必须的参数,
     * 测试URL:http://localhost:8080/v1/test2?size=2&form=22&&page=33   
     * http://localhost:8080/v1/test2?size=2 
     * http://localhost:8080/v1/test2?size=2&form=22
     * http://localhost:8080/v1/test2?size=2&page=33
     * @param from 传入参数,name传入参数值放入form,如果name为null默认值为0
     * @param size
     * @return
     */
    @GetMapping(value="/v1/test2")
    public Object findUser4(@RequestParam(defaultValue = "0",name = "page")int  from, int size) {
        map.clear();
        map.put("from", from);
        map.put("size", size);      
        return map;
    }
    /**
     * @RequestBody: 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
     * 在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
     * http://localhost:8080/v1/test3?sex=女&name=lxh
     * @param user
     * @return
     */
    @RequestMapping(value="/v1/test3",method = RequestMethod.POST)
    public Object findUser5(@RequestBody User user) {
        map.clear();
        map.put("user", user);  
        return map;
    }
    /**
     * 功能描述:测试获取http头信息
     * http://localhost:8080/v1/test4?id=1 注意此时需要再header中添加access_token参数
     * @param accesstoken
     * @param id
     * @return
     */
    @GetMapping(value="/v1/test4")
    public Object findUser6(@RequestHeader("access_token")String accesstoken,String id) {
        map.clear();
        map.put("access_token", accesstoken);   
        map.put("id", id);  
        return map;
    }
    /**
     * http://localhost:8080/v1/test_request?id=123
     * @param request
     * @return
     */
    @GetMapping("/v1/test_request")
    public Object testRequest(HttpServletRequest request){
        map.clear();
        String id = request.getParameter("id");
        map.put("id", id);
        return map; 
    }
}

相关文章

  • 3.注解

    1.@RestController =@Controller+@ResponseBody @ResponseBod...

  • 3.类型注解

    3.1 什么是类型注解 演示代码: let age: number = 18; 代码中的 :number 就是...

  • 使用 “参数注解-@loginUser” 获取登陆用户信息

    1.登录校验注解【@Login】 2.登录用户信息注解【@LoginUser 】 3.有@LoginUser注解的...

  • Hibernate学习笔记(四) 注解

    目录 1.注解步骤2.类的注解3.属性的注解4.关系的注解5.注解与XML 一、注解步骤 以实体类Product为...

  • Android 面试要点之注解

    1.注解的使用 2. 注解的级别及意义 3. 如何自定义注解 参考资料

  • 5.注解

    1.注解的概念 2.常见的注解 3.定义注解语法 4.带有属性的注解 5.注解注意的细节: 5.元注解的概念(在注...

  • asp处理点击触发

    1.新建注解@ClickAnn 2.处理逻辑 3.在controller层加注解使用

  • 3. SpringMVC常用注解

    准备 1. RequestParam 作用:把请求中指定名称的参数给控制器中的形参赋值。属性:value:请求参数...

  • 3.注解的使用

    一、注解相关基础概念: 1.Annotation注解是JDK1.5之后才有的新特性。 2.元注解 @Retenti...

  • SpringBoot 自定义注解

    1.定义一个注解 2.编写切面文件 3.使用注解 4.效果

网友评论

    本文标题:3.注解

    本文链接:https://www.haomeiwen.com/subject/xfzfjhtx.html