SpringMVC提供@RestController Restful风格

在使用Restful风格的SpringMVC时,为了不用在每个功能方法上都要添加上@ResponseBody,SpringMVC提供了@RestController

下面是注解的定义:

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.stereotype.Controller;

/**  * A convenience annotation that is itself annotated with {@link Controller @Controller}  * and {@link ResponseBody @ResponseBody}.  * <p>  * Types that carry this annotation are treated as controllers where  * {@link RequestMapping @RequestMapping} methods assume  * {@link ResponseBody @ResponseBody} semantics by default.  *  * @author Rossen Stoyanchev  * @author Sam Brannen  * @since 4.0  */ @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented @Controller @ResponseBody public @interface RestController {

   /**  * The value may indicate a suggestion for a logical component name,  * to be turned into a Spring bean in case of an autodetected component.  * @return the suggested component name, if any  * @since 4.0.1  */  String value() default "";

}
可以看到已经为我们提供了@Controller和@ResponseBody

以后直接在controller类上加上@RestController

@Controller注明类是控制器类

@ResponseBody 会调用配置的HttpMessageConverter 对处理器方法返回的对象进行转换

你可能感兴趣的:(SpringMVC提供@RestController Restful风格)