当前位置: 代码网 > it编程>编程语言>Java > SpringBoot接受参数相关注解方式

SpringBoot接受参数相关注解方式

2024年12月10日 Java 我要评论
springboot接受参数相关注解1.基本介绍2.@pathvariable 路径参数获取信息1.代码实例1.index.html<!doctype html><html lang

springboot接受参数相关注解

1.基本介绍

2.@pathvariable 路径参数获取信息

1.代码实例

1.index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/monster/100/king" rel="external nofollow" >@pathvariable-路径变量:/monster/100/king</a>
</body>
</html>

2.parametercontroller.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.restcontroller;

import java.util.map;

/**
 * @author 孙显圣
 * @version 1.0
 */
@restcontroller
public class parametercontroller {

    @getmapping("/monster/{id}/{name}") //接受两个路径参数
    public string pathvariable(@pathvariable("id") integer id, @pathvariable("name") string name,
                               @pathvariable map<string, string> map) { //这里的map指将所有的路径参数都放到map中
        system.out.println("id:" + id + " name:" + name);
        for (map.entry<string, string> entry : map.entryset()) {
            system.out.println("key:" + entry.getkey() + " value: " + entry.getvalue());
        }
        return "success"; //返回json给浏览器
    }

}

3.测试

2.细节说明

  • @pathvariable(“xxx”)必须跟{xxx}相对应
  • 可以将所有的路径参数放到map中 @pathvariable map<string, string> map

3.@requestheader 请求头获取信息

1.代码实例

1.index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/requestheader" rel="external nofollow" >@requestheader-获取请求头信息</a>
</body>
</html>

2.parametercontroller.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestheader;
import org.springframework.web.bind.annotation.restcontroller;

import java.util.map;

/**
 * @author 孙显圣
 * @version 1.0
 */
@restcontroller
public class parametercontroller {

    @getmapping("/requestheader") //获取请求头的信息
    public string requestheader(@requestheader("host") string host, @requestheader map<string, string> header) {
        system.out.println("host:" + host);
        system.out.println(header);
        return "success";
    }

}

3.测试

2.细节说明

  • 请求头的信息都是以key - value的形式存储的
  • 可以通过@requestheader(“xxx”)来获取xxx对应的value
  • 也可以通过@requestheader map<string, string> header将所有的key - value都封装到map中

4.@requestparameter 请求获取参数信息

1.代码实例

1.index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/hi?hobby=打篮球&hobby=踢球" rel="external nofollow" >@requestparam-请求参数</a>
</body>
</html>

2.parametercontroller.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;

import java.util.list;

/**
 * @author 孙显圣
 * @version 1.0
 */
@restcontroller
public class parametercontroller {

    @getmapping("/hi")
    public string hi(@requestparam(value = "name", defaultvalue = "孙显圣") string name,
                     @requestparam("hobby") list<string> list) {
        system.out.println("name:" + name);
        system.out.println(list);
        return "success";
    }


}

3.测试

2.细节说明

  • 请求参数是可以设置默认值的,使用defaultvalue属性即可
  • 请求参数还可以将同名的结果封装到list中
  • 请求参数也可以使用@requestparameter map<string, string> map 将所有参数封装到map中,但是如果有同名的结果只会得到第一个,因为map的key是唯一的

5.@cookievalue cookie获取值

1.代码实例

1.index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<a href="/cookie" rel="external nofollow" >@cookievalue-获取cookie的值</a>
</body>
</html>

2.parametercontroller.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.cookie;
import javax.servlet.http.httpservletrequest;

/**
 * @author 孙显圣
 * @version 1.0
 */
@restcontroller
public class parametercontroller {

    @getmapping("/cookie")
    //这里可以设置required = false意为不是必须存在的,如果不存在则得到的值就为null
    //如果后面的参数类型是cookie,则会获取cookie对象并封装到变量中
    public string cookie(@cookievalue(value = "cookie_key", required = false) string cookie_value,
                         @cookievalue(value = "username" , required = false) cookie cookie, httpservletrequest request) {
        //使用原生api获取cookies
        cookie[] cookies = request.getcookies();
        for (cookie cookie1 : cookies) {
            system.out.println(cookie1);
        }

        system.out.println(cookie_value);
        system.out.println("name:" + cookie.getname() + " value: " + cookie.getvalue());

        return "success";
    }


}

3.测试

2.细节说明

  • @cookievalue可以根据后面要封装的参数的类型来获取指定的值,如果后面的类型是cookie类型则会获取一个cookie对象并封装进入,如果是string类型则会获取cookie的value来进行封装
  • 还可以通过servlet原生api的request来获取所有的cookie
  • @cookievalue中有属性required默认为true,意为必须存在,否则报错,如果设置为false,则如果获取不到则为null

6.@requestbody 处理json请求,post请求体获取信息

1.代码实例

1.index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
<h1>基本注解</h1>
<hr/>
<form action="/requestbody" method="post">
    <input type="text" name="username"><br>
    <input type="text" name="password"><br>
    <input type="submit" value="submit">
</form>
</body>
</html>

2.parametercontroller.java

package com.sun.springboot.controller;

import org.springframework.web.bind.annotation.*;



/**
 * @author 孙显圣
 * @version 1.0
 */
@restcontroller
public class parametercontroller {

    @postmapping("requestbody")
    public string getrequestbody(@requestbody string requestbody) { //获取请求体
        system.out.println(requestbody);
        return "success";
    }

}

3.测试

7.@requestattribute 请求域获取信息

1.代码实例

1.requestcontroller.java

package com.sun.springboot.controller;

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestattribute;
import org.springframework.web.bind.annotation.responsebody;

import javax.servlet.http.httpservletrequest;
/**
 * @author 孙显圣
 * @version 1.0
 */
@controller
public class requestcontroller {

    @getmapping("/login")
    public string login(httpservletrequest request) {
        //在request域中存放一些信息
        request.setattribute("name", "sun");
        request.setattribute("age", 13);
        //调用视图解析器,请求转发到/ok
        return "forward:/ok";
    }

    @responsebody
    @getmapping("/ok")
    public string ok(@requestattribute(value = "name", required = false) string name) { //使用注解来获取请求域中的信息并封装到参数中
        system.out.println("name: " + name);
        return "success"; //返回json给浏览器
    }
}

2.配置视图解析器 application.yml

spring:
  mvc:
    view: #配置了视图解析器
      suffix: .html #后缀
      prefix: / #前缀,指的是根目录

3.测试

8.@sessionattribute session域获取信息

1.代码实例

1.sessioncontroller.java

    package com.sun.springboot.controller;
    
    import org.springframework.stereotype.controller;
    import org.springframework.web.bind.annotation.getmapping;
    import org.springframework.web.bind.annotation.responsebody;
    import org.springframework.web.bind.annotation.sessionattribute;
    
    import javax.servlet.http.httpservletrequest;
    /**
     * @author 孙显圣
     * @version 1.0
     */
    @controller
    public class sessioncontroller {
    
        @getmapping("/login")
        public string login(httpservletrequest request) {
            //在session域中设置信息
            request.getsession().setattribute("session", "session_value");
    
            //调用视图解析器,请求转发到/ok
            return "forward:/ok";
        }
    
        @responsebody
        @getmapping("/ok")
        public string ok(@sessionattribute(value = "session") string value) { //使用注解来获取session域中的信息并封装到参数中
            system.out.println("session: " + value);
            return "success"; //返回json给浏览器
        }
    }

2.配置视图解析器(同上)

3.测试

9.复杂参数

1.代码实例

1.requestcontroller.java

package com.sun.springboot.controller;

import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestattribute;
import org.springframework.web.bind.annotation.responsebody;

import javax.servlet.http.httpservletresponse;
import java.util.map;

/**
 * @author 孙显圣
 * @version 1.0
 */
@controller
public class requestcontroller {

    @getmapping("/login")
    public string login(map<string, object> map, model model, httpservletresponse response) {
        //给map封装信息
        map.put("user", "sun");
        map.put("job", "工程师");

        //model封装信息
        model.addattribute("sal", 1000);

        //结果最后都会封装到request域中

        //调用视图解析器,请求转发到/ok
        return "forward:/ok";
    }

    @responsebody
    @getmapping("/ok")
    public string ok(@requestattribute("user") string user, @requestattribute("job") string job,
                     @requestattribute("sal") integer sal) { //使用注解来获取请求域中的信息并封装到参数中
        system.out.println("user:" + user + " job:" + job + " sal:" +sal);
        return "success"; //返回json给浏览器
    }
}

2.测试

2.httpservletresponse给浏览器设置cookie

1.代码实例

package com.sun.springboot.controller;

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.cookievalue;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.responsebody;

import javax.servlet.http.cookie;
import javax.servlet.http.httpservletresponse;

/**
 * @author 孙显圣
 * @version 1.0
 */
@controller
public class requestcontroller {

    @getmapping("/login")
    public string login(httpservletresponse response) {
        cookie cookie = new cookie("cookie_name", "cookie_value");
        response.addcookie(cookie);

        //调用视图解析器,重定向到/ok,不能使用请求转发,因为虽然响应给客户端cookie了,
        // 但是由于是请求转发,第二个controller得到的是最开始的请求,那时候还没有cookie
        return "redirect:/ok";
    }

    @responsebody
    @getmapping("/ok")
    public string ok(@cookievalue("cookie_name") cookie cookie) {
        //获取cookie
        system.out.println("key:" + cookie.getname() + " value:" + cookie.getvalue());
        return "success"; //返回json给浏览器
    }
}

2.测试

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com