sping boot demo解释

来源:互联网 发布:网络协议怎么设置 编辑:程序博客网 时间:2024/06/07 11:22

      

      Spring boot设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置

    用spring的方法来构建RESTful Web服务,HTTP请求是由controller处理,这些组件可以由@ restcontroller注解

     

@RestControllerpublic class GreetingController {    private static final String template = "Hello, %s!";    private final AtomicLong counter = new AtomicLong();    @RequestMapping("/greeting")    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {        return new Greeting(counter.incrementAndGet(),                            String.format(template, name));    }}
    注解@requestMapping 默认支持post,get,put,可以使用@RequestMapping(method=GET)

   @ RequestParam值在greeting()方法名称参数,这个查询字符串参数不是必需的;如果它在请求缺席,默认值会被使用。这个RESTful Web服务          controller简单地返回一个请求的对象,然后对象被直接写入HTTP响应JSON格式中。此代码使用spring4的新”restcontroller注释,这注解标志作为一    个控制器,每一个方法返回一个对象而不是一个view,这个注解包括了@ responsebody,@controller。请求的对象必须被转换成JSON,感谢spring    的HTTP消息转换器支持,你不需要手动做这种转换。因为Jakeson 2在classp中,spring的mappingjackson2httpmessageconverter自动选择转换到     JSON实例。

传统上市打包成war部署到服务器中,下面演示创建一个独立的application。把一切打包到一个可执行jar文件中,由一个Java main()方法驱动。并 且,也可以使用spring支持的embedding the Tomcat servlet container运行。

@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}
@SpringBootApplication 注解包括

   @Configuration  the application context 定义beans

   @EnableAutoConfiguration 告诉spring启动开始添加基于路径的配置的beans,其他beans,及各种设置。

   你通常会为Spring MVC应用添加@EnableWebMvc,但spring boot自动添加,当它看到Springwebmvc在classpath。这个标志中的应用作为一个Web应用程序,并激活如设置DispatcherServlet。 

      @ComponentScan告诉spring寻找其他组件,配置,和services(在hello包中),让它找到hellocontroller 

      main()使用spring boot的springapplication run()方法来启动应用程序。你有没有注意到没有一条XML?没有web.xml文件。这个web应用程序是100%纯的java。


 http://localhost:8080/greeting

{"id":1,"content":"Hello, World!"}
http://localhost:8080/greeting?name=User
{"id":2,"content":"Hello, User!"}

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>org.springframework</groupId>    <artifactId>gs-rest-service</artifactId>    <version>0.1.0</version>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.2.5.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <properties>        <java.version>1.7</java.version>    </properties>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>    <repositories>        <repository>            <id>spring-releases</id>            <url>https://repo.spring.io/libs-release</url>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>spring-releases</id>            <url>https://repo.spring.io/libs-release</url>        </pluginRepository>    </pluginRepositories></project>

      





0 0
原创粉丝点击