记录-SpringMVC4常用注解及详解

来源:互联网 发布:准提法网络佛学院 编辑:程序博客网 时间:2024/06/18 04:26

Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam, @ModelAttribute等等这样类似的注解。到目前为止,Spring的版本虽然发生了很大的变化,但注解的特性却是一直延续下来,并不断扩展,让广大的开发人员的双手变的更轻松起来,这都离不开Annotation的强大作用,今天我们就一起来看看Spring MVC 4中常用的那些注解吧。
1. @Controller
Controller控制器是通过服务接口定义的提供访问应用程序的一种行为,它解释用户的输入,将其转换成一个模型然后将试图呈献给用户。Spring MVC 使用 @Controller 定义控制器,它还允许自动检测定义在类路径下的组件并自动注册。如想自动检测生效,需在XML头文件下引入 spring-context:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan base-package="org.springframework.samples.petclinic.web"/>    <!-- ... --></beans>

2. @RequestMapping
我们可以 @RequestMapping 注解将类似 “/favsoft”这样的URL映射到整个类或特定的处理方法上。一般来说,类级别的注解映射特定的请求路径到表单控制器上,而方法级别的注解只是映射为一个特定的HTTP方法请求(“GET”,“POST”等)或HTTP请求参数。

@Controller@RequestMapping("/favsoft")public class AnnotationController {    @RequestMapping(method=RequestMethod.GET)    public String get(){        return "";    }    @RequestMapping(value="/getName", method = RequestMethod.GET)    public String getName(String userName) {        return userName;    }    @RequestMapping(value="/{day}", method=RequestMethod.GET)    public String getDay(Date day){        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");        return df.format(day);    }    @RequestMapping(value="/addUser", method=RequestMethod.GET)    public String addFavUser(@Validated FavUser favUser,BindingResult result){        if(result.hasErrors()){            return "favUser";        }        //favUserService.addFavUser(favUser);        return "redirect:/favlist";    }    @RequestMapping("/test")    @ResponseBody    public String test(){        return "aa";    }}```  @RequestMapping 既可以作用在类级别,也可以作用在方法级别。当它定义在类级别时,标明该控制器处理所有的请求都被映射到 /favsoft 路径下。@RequestMapping中可以使用 method 属性标记其所接受的方法类型,如果不指定方法类型的话,可以使用 HTTP GET/POST 方法请求数据,但是一旦指定方法类型,就只能使用该类型获取数据。  @RequestMapping 可以使用 @Validated与BindingResult联合验证输入的参数,在验证通过和失败的情况下,分别返回不同的视图。   @RequestMapping支持使用URI模板访问URL。URI模板像是URL模样的字符串,由一个或多个变量名字组成,当这些变量有值的时候,它就变成了URI。  **3. @PathVariable**      在Spring MVC中,可以使用 @PathVariable 注解方法参数并将其绑定到URI模板变量的值上。如下代码所示:<div class="se-preview-section-delimiter"></div>

String findOwner( String , Model model) {
FavUser favUser = favUserService.findFavUser();
model.addAttribute(
;
}

 URI模板 “favusers/{favUserId}"指定变量的名字 favUserId ,当控制器处理这个请求的时候, favUserId的值会被设定到URI中。比如,当有一个像“favusers/favccxx”这样的请求时,favUserId的值就是 favccxx。@PathVariable 可以有多个注解,像下面这样:<div class="se-preview-section-delimiter"></div>

@RequestMapping(value=”/owners/{ownerId}/pets/{petId}”, method=RequestMethod.GET)public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
Pet pet = owner.getPet(petId);
model.addAttribute(“pet”, pet); return “displayPet”;
}

如果@PathVariable使用Map<String, String>类型的参数时, Map会填充到所有的URI模板变量中。@PathVariable支持使用正则表达式,这就决定了它的超强大属性,它能在路径模板中使用占位符,可以设定特定的前缀匹配,后缀匹配等自定义格式。 @PathVariable还支持矩阵变量,因为现实场景中用的不多,这就不详细介绍了,有需要的童鞋请查看官网的文档。**4.@RequestParam **   @RequestParam将请求的参数绑定到方法中的参数上,如下面的代码所示。其实,即使不配置该参数,注解也会默认使用该参数。如果想自定义指定参数的话,如果将@RequestParam的 required 属性设置为false(如@RequestParam(value="id",required=false))。   **5. @RequestBody**    @RequestBody是指方法参数应该被绑定到HTTP请求Body上。<div class="se-preview-section-delimiter"></div>

@RequestMapping(value = “/something”, method = RequestMethod.PUT)public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}

  如果觉得@RequestBody不如@RequestParam趁手,我们可以使用 HttpMessageConverter将request的body转移到方法参数上, HttMessageConverser将 HTTP请求消息在Object对象之间互相转换,但一般情况下不会这么做。事实证明,@RequestBody在构建REST架构时,比@RequestParam有着更大的优势。  **6. @ResponseBody**      @ResponseBody@RequestBody类似,它的作用是将返回类型直接输入到HTTP response body中。@ResponseBody在输出JSON格式的数据时,会经常用到,代码见下:<div class="se-preview-section-delimiter"></div>

@RequestMapping(value = “/something”, method = RequestMethod.PUT)@ResponseBodypublic String helloWorld() { return “Hello World”;
}

**7. @RestController**  我们经常见到一些控制器实现了REST的API,只为服务于JSON,XML或其它自定义的类型内容,@RestController用来创建REST类型的控制器,与@Controller类型。@RestController就是这样一种类型,它避免了你重复的写@RequestMapping@ResponseBody。<div class="se-preview-section-delimiter"></div>

@RestController
public class FavRestfulController {

@RequestMapping(value=”/getUserName”,method=RequestMethod.POST)
public String getUserName(@RequestParam(value=”name”) String name){
return name;
}
}

** 8. HttpEntity**   HttpEntity除了能获得request请求和response响应之外,它还能访问请求和响应头,如下所示:<div class="se-preview-section-delimiter"></div>

@RequestMapping(“/something”)public ResponseEntity handle(HttpEntity

原创粉丝点击