Spring bootの最初の例

11268 ワード

Spring bootはとても良いフレームワークで、この例では彼の簡単さ、配置の簡単さを感じました.私はmavenで開発しているのでspring bootのmaven方式を使っています.pom.xml:
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.sunhuwh.springbootgroupId>
  <artifactId>spring-boot-demoartifactId>
  <packaging>jarpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>spring-boot-demo Maven Webappname>
  <url>http://maven.apache.orgurl>

    <properties>
        <java.version>1.8java.version>
    properties>

    
    <parent>  
        <groupId>org.springframework.bootgroupId>  
        <artifactId>spring-boot-starter-parentartifactId>  
        <version>1.4.1.BUILD-SNAPSHOTversion>
    parent>  

      
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.bootgroupId>  
            <artifactId>spring-boot-starter-webartifactId>  
        dependency>  
    dependencies>  

      
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>

      
      
    <repositories>
        <repository>
            <id>spring-snapshotsid>
            <url>http://repo.spring.io/snapshoturl>
            <snapshots><enabled>trueenabled>snapshots>
        repository>
        <repository>
            <id>spring-milestonesid>
            <url>http://repo.spring.io/milestoneurl>
        repository>
    repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshotsid>
            <url>http://repo.spring.io/snapshoturl>
        pluginRepository>
        <pluginRepository>
            <id>spring-milestonesid>
            <url>http://repo.spring.io/milestoneurl>
        pluginRepository>
    pluginRepositories>
project>

アプリケーション:起動
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//@Configuration
//@EnableAutoConfiguration
//@ComponentScan
@SpringBootApplication//      
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Web:@RestController+@RequestMappingを使用すると、RESTful webサービスを開発する目的を達成できます.responseはresponse bodyを通じて送信されるだけです.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sunhuwh.springboot.service.DemoService;

@RestController
@RequestMapping
public class IndexController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("/")
    public String index(){
        demoService.test();
        return "hello world!";
    }

}