spring注解二

请求参数

两种方式:

 @RequestMapping("/hello1")
    @ResponseBody
    public ResData hello1(String username, String password, Integer age, Date birthday) {
        System.out.println(username);
        System.out.println(password);
        System.out.println(age);
        System.out.println(birthday);
        return new ResData(200,"ok",null);
    }
    @RequestMapping("/hello2")
    @ResponseBody
    public ResData hello2(User user) {
        System.out.println(user);
        return new ResData(200,"ok",null);
    }

请求

请求/hello就会由这个方法响应,ResData转换成JSON给前端

1.@RequestMapping("/hello")

既能标注在类上(一级目录),又可以标注在方法上(二级目录)

method = RequestMethod.POST,可以控制请求的方法,只允许 Post

@Controller
//请求的一级目录
@RequestMapping("/user")
public class UserController {
    //请求的二级目录,不写method = RequestMethod.xxx则默认什么请求都允许。
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResData delete(Integer id){
        return new ResData(200,"ok","删除"+id+"成功");
    }
}

@GetMapping("/xxx")//查询

@PostMapping ("/xxx")//添加

@PutMapping("/xxx")//修改

@DeleteMapping("/xxx")//删除

2.@RequestParam

①控制传参是否必须 默认时true,加上(required = false)则为false

②控制默认值@RequestParam(defaultValue = " ")默认值,主要用在分页上

③@RequestParam(”xxx“)String yyy此时yyy将识别不出来,可以先将属性名为xxx的数据传到xxx再转到yyy

    public ResData hello3(@RequestParam(required = false,defaultValue = "1") User user)


//@RequestParam 控制传参是否必须 默认时true,加上(required = false)则为false
    public ResData hello3(@RequestParam(required = false) User user) {

//@RequestParam(defaultValue = "1")默认值,主要用在分页上
    public ResData hello3(@RequestParam(defaultValue = "1") Interger pageNum)


// @RequestParam(”xxx“)String yyy先将属性名为xxx的数据传到xxx再转到yyy
    public ResData hello3(@RequestParam("bookname")String name)

3.@PathVariable路径变量

 @RequestMapping("/goods/queryById/{id}")
    @ResponseBody
    public ResData queryById(@PathVariable("id") Integer id) {
        System.out.println(id);
        return new ResData(200,"ok",null);
    }

4.@RequestBody

//axios.post("/user/login","username="+this.username+"&password="+this.password)
                let param = new URLSearchParams();
                param.append("username",this.username)
                param.append("password",this.password)
                // 传参的方式接收使用login(User user)
                // axios.post("/user/login",param)

                //对象的方式接收使用login(@RequestBody User user)
                // axios.post("/user/login",{username:this.username, password:this.password})
                //当数据多时创建一个对象将数据都放进去然后接受方式也是login(@RequestBody User user)
                let user = {};
                user.username=this.username;
                user.password=this.password;
                axios.post("/user/login",user)

5.@CookieValue(了解)

6.@RequestHeader(了解)

7.如何使用原生请求响应

@RequestMapping("/set")
@ResponseBody
public ResData set(HttpServletRequest request, HttpServletResponse response,HttpSession session)
session.setAttribute( s:"name",o:"张三丰"):
return new ResData( code: 200, msg: "ok", data: null) ;

@RequestMapping("/get")
@ResponseBody
public ResData get(HttpservletReauest reauest, HttpservletResponse response,Httpsession session)
return new ResData( code: 200, msg: "ok" ,session.getAttribute( s: "name")):

响应相关

1.@ResponseBody

把方法的的返回值直接转换成JSON,给前端

@RequestMapping(value = "/delete")
    @ResponseBody
    public ResData delete(Integer id) {
        return new ResData(200, "ok", "删除" + id + "成功");
    }

2.响应一个页面

 //给前端一个页面
    @RequestMapping("/loginPage")
    public  String loginPage(){
        //templates/login.html
        return "login";
    }

3.响应一个页面带数据过去

//给前端一个数据+页面
    @RequestMapping("/listPage")
    public ModelAndView listPage(){
        //templates/list.html
        ModelAndView mv = new ModelAndView("list");
        mv.addObject("id",100);
        return mv;
    }

4.自己响应,返回值为void

 //不让springmvc插手自己来响应,返回值void
    @RequestMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().write("hello!!!!!!");
    }

@SpringBootApplication

补充注解

@RestController
等于@Controller+@ResponseBody
表示这个类里面所有的方法,都响应]SON

springboot是怎么创建的

springboot的目录结构

spring的16个注解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值