创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

本文简介使用IDE和无IDE 创建spring boot的RESTful Web Service工程。

开始前你需要:配置好java 环境变量,maven环境变量(自行搜索安装方式),idea(若要使用IntelliJ IDEA创建工程)。

一、无IDE

1、创建工程目录:

创建如下工程目录,其中src-main-java为固定maven结构。test为项目根目录。java下一级目录名称可以自定义(如springboot)。

test>src>main>java>springboot

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

2、在项目根目录test添加pom.xml的maven文件内容如下:

<?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.5.9.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </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>

test根目录结构如下:

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

3、在test>src>main>java>springboot目录下添加3个类如下

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

其中Boot为springboot启动类

package springboot;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Boot {


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

Hello.java:应答bean

package springboot;


public class Hello {


    private final long id;
    private final String content;


    public Hello(long id, String content) {
        this.id = id;
        this.content = content;
    }


    public long getId() {
        return id;
    }


    public String getContent() {
        return content;
    }
}

HelloController.java为控制类

package springboot;


import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {


    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();


    @RequestMapping("/hello")
    public Hello Hello(@RequestParam(value="name", defaultValue="World") String name) {
        return new Hello(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

3、编译运行:

打开cmd,切换到项目根目录test。运行 mvn spring-boot:run

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

得到以下结果那么恭喜运行成功了:

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

在浏览器访问http://localhost:8080/hello

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

二、使用IntelliJ IDEA创建工程

1、File-new project创建如下图工程

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)


创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

2右键工程添加Framework(maven)支持

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

在src.main.java下添加springboot目录,删除main.java

至此工程结构创建完毕如下

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

修改pom文件内容如下:

<?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.5.9.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </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>


在src.main.java.springboot目录下添加以下三个类:

Hello.java

package springboot;


public class Hello {


    private final long id;
    private final String content;


    public Hello(long id, String content) {
        this.id = id;
        this.content = content;
    }


    public long getId() {
        return id;
    }


    public String getContent() {
        return content;
    }
}

HelloController.java

package springboot;


import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {


    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();


    @RequestMapping("/hello")
    public Hello Hello(@RequestParam(value="name", defaultValue="World") String name) {
        return new Hello(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

Boot.java:

package springboot;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Boot {


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

配置运行参数:

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

Name随意Command line 填入spring-boot:run

保存。

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

 运行springtest  debug

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

结果报错了maven 未配置

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

那么前往设置一下:

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

maven在idea的环境变量设置如下

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

另类Runner下的maven的VM Options 设为:-Dmaven.multiModuleProjectDirectory=$M2_HOME

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

再次运行

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

启动成功,同样用浏览器访问http://localhost:8080/hello得到如下结果:

创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)

以上为创建maven spring boot 的 RESTful Web Service的方法,主要参考spring官方资料。如有错误以及建议请指正感谢。

贴上源码http://download.csdn.net/download/zhangbing2434/10200124  本来要选无积分下载的。可是不知怎么选不了。。。