springmvc中的注解

@Value注入Properties数据

-------------------------------------------------------------------------------

@PathVariable是用来获得请求url中的动态参数的

----------------------------------------------------------------------------------------------

要返回json数据要使用@ResponseBody
------------------------------------------------------------------------------------
public String queryUserName(@RequestParam String userName)
请求中包含username参数(如/requestparam1?userName=zhang),则自动传入。
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
defaultValue:默认值,表示如果请求中没有同名参数时的默认值

public String requestparam5(  @RequestParam(value="username", required=true, defaultValue="zhangsan") String username)

如果没有传入参数,则默认是"zhangsan".

-------------------------------------------------------------------------------------------------------

javax.inject中@Inject、@Named、@Qualifier和@Provider用法

-------------------------------------------------------------------------------------------------------

 

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

//定义两个路径
@RequestMapping(path  ={"/","/index"})
@ResponseBody
public String index(){
    return "Hello reed";
}

http://localhost:8080/  和http://localhost:8080/index的效果一样

 

---------------------------------------------------------------------------------------------------------------

 

//@PathVarible,@RequestParam,defaultValue,required的用法
@RequestMapping(path = {"profile/{groupId}/{userId}"})
@ResponseBody
public String profile(@PathVariable("userId") int userId, @PathVariable("groupId") String groupId,
                      @RequestParam(value = "type",defaultValue = "1") int type,
                      @RequestParam(value = "key",required = false) String key){
    return String.format("profile %s/%d,t:%d k:%s",groupId,userId,type,key);
}
//http://localhost:8080/profile/1/1?type=2&key=reed    profile 1/1,t:2 k:reed
//http://localhost:8080/profile/1/1?key=reed   profile 1/1,t:1 k:reed
//http://localhost:8080/profile/1/1     profile 1/1,t:1 k:null

 

 

 

@PathVarible取路径中的参数做为变量参数

?后面那种带参可以用@RequestParam

defaultValue用来设置默认值

上一篇:后台报错"Optional int parameter 'page' is present but cannot be translated into a null va


下一篇:web层controller中常用注解