Spring Boot

1. @RestController combines @Controller and @ResponseBody, 这是不是意味着不用再import jakson的包(@ResponseBody时用到的,将对象转成json对象)了? 有待考证。

2. Create an Application class

 1 package hello;
 2 
 3 import java.util.Arrays;
 4 
 5 import org.springframework.boot.SpringApplication;
 6 import org.springframework.boot.autoconfigure.SpringBootApplication;
 7 import org.springframework.context.ApplicationContext;
 8 
 9 @SpringBootApplication
10 public class Application {
11 
12     public static void main(String[] args) {
13         ApplicationContext ctx = SpringApplication.run(Application.class, args);
14 
15         System.out.println("Let's inspect the beans provided by Spring Boot:");
16 
17         String[] beanNames = ctx.getBeanDefinitionNames();
18         Arrays.sort(beanNames);
19         for (String beanName : beanNames) {
20             System.out.println(beanName);
21         }
22     }
23 
24 }

以上代码,调用annotation @SpringBootApplication,它会加载@Configuration,@EnableAutoConfiguation,@ComponentScan

其中,@Configuration:标记该class是ApplicationContext的一个bean定义,相当于原来applicationContext.xml文件中的<bean>标签。

   @EnableAutoConfiguation:标记Spring Boot开始加载各种beans(包括自定义的,classpath中的),以及各种属性。

     @ComponentScan:告知Spring去搜索其他组件,配置或者服务。其实就是原来applicationContext.xml文件中的<mvc:component-scan>标签。

另外,由以上分析,个人认为在Spring Boot中,实际上是通过各种annotation来代替.xml配置文件中的各种配置,从而将xml文件干掉。

针对Spring MVC应用,本应该需要注解@EnableWebMvc,但是当Spring Boot在classpath中看到spring-webmvc时,就会自动加载该注解,激活DispatcherServlet等。

    问题:对DispatcherServlet的配置,如何通过Spring Boot来配置呢?

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

通过对 Spring Boot中  Building a RESTful Web Service  这一章节的阅读,有以下理解:

首先,解决问题一:@RestController combines @Controller and @ResponseBody......

  仍然需要import jakson的包,因为spring并没有提供将java Object转成JSON对象的package。

  但spring提供MappingJackson2HttpMessageConverter,从而通过监测到jakson的包而自动加载并使用。最终将java Object转成JSON对象并附于HTTP response。

其次,如下:

 1 package hello;
 2 
 3 import java.util.concurrent.atomic.AtomicLong;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestParam;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 public class GreetingController {
10 
11     private static final String template = "Hello, %s!";
12     private final AtomicLong counter = new AtomicLong();
13 
14     @RequestMapping("/greeting")
15     public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
16         return new Greeting(counter.incrementAndGet(),
17                             String.format(template, name));
18     }
19 }

终于明白@RequestMapping, @ResponseBody的用意和区别了。

可以说,@ResponseBody是为RESTful web service服务的。正如上所述,traditional mvc controller和RESTful web service controller之间的区别主要在于HTTP响应体的创建方式。

前者应用在Spring中,具体表现为在处理请求后,展示数据将存于map/modelMap/viewAndModelMap中,返回的是String/void类型(String则表示view的名称),再通过view技术对model数据进行渲染。

后者则不同,而是将展示数据存于自定义的对象中,并返回该对象,最终将该对象转成JSON对象附于HTTP响应体上。类似于MVC被VC替代了。

在Spring 4中,增加了@RestController注解,它是@Controller和@ResponseBody的结合。用于标记该类中的所有方法将返回一个自定义对象,而非视图。所以,当需要Controller既返回view又返回对象时,就不能使用@RestController了。

清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
原文地址:https://www.cnblogs.com/hello-yz/p/4592855.html