Spring Boot: Building a RESTful Web Service

来源:互联网 发布:淘宝广告商品被删 编辑:程序博客网 时间:2024/05/20 14:16

1、@RestController:
which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.

2、在springBoot的controller中返回一个对象,不需要手动转换为json,因为会自动转换:
Because Jackson 2 is on the classpath, Spring’s MappingJackson2HttpMessageConverter is automatically chosen to convert the Greeting instance to JSON.

3、@SpringBootApplication注解综合了如下几个注解的功能
@Configuration tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.(注意:这里的英文注释中hello这个包,是springBoot的main方法中使用@SpringBootApplication注解类所在的包,也就是说,会自动扫描,但是只会扫描此包下以及子包下的类,所有会报错

4、Build an executable JAR

  1. 如何将项目打包为一个jar文件:If you are using Maven, you can run the application using mvn spring-boot:run. Or you can build the JAR file with mvn clean package,Then you can run the JAR file:
    java -jar target/gs-rest-service-0.1.0.jar
原创粉丝点击