zoukankan      html  css  js  c++  java
  • 冷注解解读

    @PostConstruct  @PreDestroy @RequestMapping ,@ResponseBody,@RequestBody,@PathVariable用法详解之地址映射
     
    RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
    RequestMapping注解有六个属性:
    1、 value, method;
    value:     指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
    method:  指定请求的method类型, GET、POST、PUT、DELETE等;
    ------------------------------------------------------------------------------------------
    2、 consumes,produces;
    consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
    produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
    ------------------------------------------------------------------------------------------
    3、 params,headers;
    params: 指定request中必须包含某些参数值是,才让该方法处理。
    headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
    ------------------------------------------------------------------------------------------
    value的uri值为以下三类:
    A) 可以指定为普通的具体值;
    B)  可以指定为含有某变量的一类值(URI Template Patterns with Path Variables);
    C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);
    -----------------------------------------------------------------------------------------
    GET模式下,这里使用了@PathVariable绑定输入参数,非常适合Restful风格。因为隐藏了参数与路径的关系,可以提升网站的安全性,静态化页面,降低恶意攻击风险。
    POST模式下,使用@RequestBody绑定请求对象,Spring会帮你进行协议转换,将JsonXml协议转换成你需要的对象。
    @ResponseBody可以标注任何对象,由Srping完成对象——协议的转换。(obj--json)
    ==========================================================================================
    code:
    @Controller 
    public class PersonController { 
     
        /**
         * 查询个人信息
         * 
         * @param id
         * @return
         */ 
        @RequestMapping(value = "/person/profile/{id}/{name}/{status}", method = RequestMethod.GET) 
        public @ResponseBody 
        Person porfile(@PathVariable int id, @PathVariable String name, // 会按照名称匹配   也就是restful风格
                @PathVariable boolean status) {                         //在get 模式下:@pathVariable用来绑定参数 
            return new Person(id, name, status); 
        } 
     
        /**
         * 登录
         * 
         * @param person
         * @return
         */ 
        @RequestMapping(value = "/person/login", method = RequestMethod.POST) 
        public @ResponseBody 
        Person login(@RequestBody Person person) { 
            return person; 
        } 
    *****************************************************************************************
    @PostConstruct 和 @PreDestroy 使用
    以上两个注释,只是作用于方法上
    @PostConstruct 注释的方法将 在类实例化后调用
    @PreDestroy  注释的方法将在类销毁之前调用
     
     
     
    -------------------------------------------------------------------------------------------------------------------------------------
    package com.chinasoft.iss;
     
     
    import java.util.Map;
     
    import javax.annotation.PostConstruct;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    /**
     *
     * @author Administrator
     * 解决方法注解不明确的地方
     */
    @Controller
    @RequestMapping("/myiss")  //此处的该标签和方法上面的该标签 有什么区别,有哪些不同
    public class MyIss0322 {
     
        @RequestMapping(value = "/ins",method = {RequestMethod.GET,RequestMethod.POST})
        @ResponseBody //此处在方法上面加这个标签作用是什么
        public void inis(String str,Map<String, Object> maps,@RequestBody java.util.List<String> lists){
            //此方法参数中,最后一个参数,为什么需要加 @RequestBody   有什么作用
        }
        @PostConstruct  //该注释表是该方法将在当前类实例化后调用
        public void init(){
     
        }
     
    }
     
     
  • 相关阅读:
    Attributes in C#
    asp.net C# 时间格式大全
    UVA 10518 How Many Calls?
    UVA 10303 How Many Trees?
    UVA 991 Safe Salutations
    UVA 10862 Connect the Cable Wires
    UVA 10417 Gift Exchanging
    UVA 10229 Modular Fibonacci
    UVA 10079 Pizza Cutting
    UVA 10334 Ray Through Glasses
  • 原文地址:https://www.cnblogs.com/lhl-shubiao/p/6685963.html
Copyright © 2011-2022 走看看