開発環境:
IED環境:日食
JDKバージョン:1.8
Mavenバージョン:3.3.9
1.スプリングブートMCV Webアプリケーションを作成します
Eclipseを開き、新しいMavenプロジェクトを作成します
QuickStartテンプレートを選択します
Mavenプロジェクトの作成を完了します
春の公式例を参照してください:http://spring.io/guides/gs/testing-web/
pom.xmlにmaven依存関係を追加します
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.github.carter659 </groupId> <artifactid> spring01 </artifactid> </biders> <name> spring01 </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-boot-starter-parent </artifactid> <version> 1.4.2.Release </version> </parent> <dependencies> <dependency> org.springframework.boot </dependency> </dependency> </dependencies> <burtion> <blugins> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-maven-plugin </artifactid>
コントローラーファイル「homecontroller.java」を追加する
パッケージcom.github.carter659.spring01; import org.springframework.steretype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.Responsobled; @RequestMapping( "/")public @responsebody string index(){return "hello、これは最初のスプリングブートアプリケーションです"; }}app.javaファイルを変更します
パッケージcom.github.carter659.spring01; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; args); }}
app.javaファイルで、Javaプログラムを実行するように右クリックして実行します
実行結果がコンソールに表示されるとき
ブラウザに「http:// localhost:8080/」を入力して、最初のスプリングブートアプリケーションにアクセスします
2。スプリングブーツのユニットテストはどのようにしますか?
pom.xmlの単体テストの依存関係を追加します
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> <scope> test </scope> </dependency>
SRC/test/javaで新しいテストクラス「httprequesttest.java」を作成します
パッケージcom.github.carter659.spring01; import static org.assertj.core.asertions.assertthat; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.Autowired; org.springframework.boot.context.embedded.localserverport; Import org.springframework.boot.test.context.springboottest; import org.springframework.boot.test.context.springboottest.webenvironment; Import; org.springframework.boot.test.web.client.TestRestTemplate;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)public class HttpRequestTest { @localserverport private int port; @autowired private testRestTemplate RESTEMPLATE; @test public void GreetingshouldReturnDefaultMessage()Throws Exception {assertthat(this.resttemplate.getForObject( "http:// localhost:" + port + "/"、string.class))。 }}ユニットテストを実行します
グリーンは、アサーションが成功していることを示しているようです
3.スプリングブートをどのように展開しますか?
これらの手順に従います。
1. Mavenをダウンロードしてください
Mavenの公式Webサイト(http://maven.apache.org/download.cgi)にMavenのBinパッケージをダウンロードしてください
2。環境変数を構成します:
これは、dディスクにmavenを解凍するプログラムファイル(x86)ディレクトリです
入力:maven_home-> d:/プログラムファイル(x86)/maven
パスに追加:;%maven_home%/bin;
CDMウィンドウに「MVN -V」コマンドを入力して、Mavenが正常にインストールされているかどうかをテストします
3.3.9バージョンはここに表示されます
3。パッキング
プログラムが配置されているディレクトリに「MVNパッケージ」コマンドを入力します(pom.xmlと同じレベル):
「Build Success」が表示されます。つまり、パッケージが成功します
パッケージ化されたJARファイルはタグGetディレクトリに表示されます
4。実行します
CMDに「Java -Jar Target/Spring01-0.0.1-Snapshot.jar」というコマンドを入力します
このプログラムは現時点で展開されています。 Spring Bootプログラムは、開発とテストが非常に簡単であるだけでなく、展開が非常に簡単であることがわかりましたか?
コードダウンロード:https://github.com/carter659/spring-boot-01.git
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。