• spring rest


    @RestController
    @RequestMapping(value = "/rest/user")
    @SessionAttributes(value = {"intValue", "stringValue"})
    public class RestUserController {
        static Map<Integer, UserInfo> users = Collections.synchronizedMap(new HashMap<Integer, UserInfo>());
    
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public List<UserInfo> getUserInfoList(){
            // 处理"/users/"的GET请求,用来获取用户列表
            // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
            List<UserInfo> list = new ArrayList<UserInfo>(users.values());
            return list;
        }
    
        @RequestMapping(value = "/", method = RequestMethod.POST)
        public String postUser(@RequestBody UserInfo userInfo){
            // 处理"/users/"的POST请求,用来创建User
            // 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数
            users.put(userInfo.getId(), userInfo);
            return "success";
        }
    
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public UserInfo getUserInfo(@PathVariable int id,
                                    @RequestHeader("Accept-Encoding") String encoding,
                                    @CookieValue("JSESSIONID") String cookie){
            System.out.println(encoding);
            System.out.println(cookie);
            // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
            // url中的id可通过@PathVariable绑定到函数的参数中
            return users.get(id);
        }
    
        //用put更新信息
        @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
        public String putUser(@PathVariable int id,@RequestBody UserInfo userInfo){
            // 处理"/users/{id}"的PUT请求,用来更新User信息
            UserInfo userInfo1 = users.get(id);
            userInfo1.setSalt(userInfo.getSalt());
            userInfo1.setName(userInfo.getName());
            userInfo1.setRole(userInfo.getRole());
            userInfo1.setPassword(userInfo.getPassword());
            return "success";
        }
    
    
        /**
         * http://localhost:9000/rest/user/1?name=test
         * 此请求才能访问得到
         * params制定了参数的值
         * header指定了需要满足的header
         * @param id
         * @return
         */
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, params = {"name=test"}, headers = {"host=localhost"})
        public String deleteUser(@PathVariable int id){
            users.remove(id);
            return "success";
        }
    
        /**
         * 会在发起请求前创建 , 方法中可以进行调用
         * @return
         */
        @ModelAttribute("hello")
        public String getModel(){
            System.out.println("-----------------hello-------------");
            return "world";
        }
    
        @ModelAttribute("intValue")
        public int getInteger(){
            System.out.println("-------------------intValue----------");
            return 10;
        }
        @ModelAttribute ( "user2" )
        public String getUser(){
            System. out .println( "---------getUser-------------" );
            return "user2";
        }
    
        /**
         * 当 @ModelAttribute 标记在方法上的时候,该方法将在处理器方法执行之前执行,然后把返回的对象存放在 session 或模型属性中,属性名称可以使用 @ModelAttribute(“attributeName”) 在标记方法的时候指定,若未指定,则使用返回类型的类名称(首字母小写)作为属性名称。关于 @ModelAttribute 标记在方法上时对应的属性是存放在 session 中还是存放在模型中,我们来做一个实验,看下面一段代码。
         * @param hello
         * @param num
         * @param user
         * @param writer
         * @param session
         * @throws IOException
         */
        @RequestMapping("/sayhello")
        public void sayHello(@ModelAttribute("hello") String hello,
                             @ModelAttribute("intValue") int num,
                             @ModelAttribute("user2") String user,
                             Writer writer,
                             HttpSession session) throws IOException{
            writer.write("Hello "+ hello+" , Hello " + user + num );
            writer.write("
    ");
            Enumeration enume = session.getAttributeNames();
            while (enume.hasMoreElements())
                writer.write(enume.nextElement() + "
    " );
    
    
        }
    
    
    
    }
  • 相关阅读:
    Pikachu漏洞平台---XSS(Cross-Site Scripting) 跨站脚本
    Pikachu漏洞练习平台(暴力破解模块)
    DVWA-- 不安全得验证码Insecure CAPTCHA
    DVWA-文件上传 File Upload
    DVWA之CSRF(跨站请求伪造)
    绑定方法与非绑定方法
    XML,面向对象基础
    configparser ,subprocess , xlrd ,xlwt 模块
    logging ,re 模块
    random,json,pickle,hashlib,shutil,hmac,shelve 模块
  • 原文地址:https://www.cnblogs.com/yixianyixian/p/7443783.html
Copyright © 2020-2023  润新知