微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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用来设置认值

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

相关推荐