今日のニュースの見出し検索
三线城市程序员老陈
、ビデオチュートリアル、または技術および生命、非常に歓迎喉の話を。
背景
Benpianは、以下のことを示します。
- SpringBootのWebプロジェクトを構成する方法
- Webインターフェイスを提供するために注釈を使用する方法
- プロファイルコンフィギュレーションポートとアクセスパスを使用する方法
プロジェクトとインポートを作成します。
それとも春Initializrを使用してプロジェクトを作成し、以下のように日食、プロジェクト情報をインポートします。
設定SpringBoot Webプロジェクト
非常にシンプルなああ、ちょうどに依存関係を追加します。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
それは非常にシンプルに見えますが、それでも若干低い理解しspring-boot-starter-web
、最終的には私たちがどのように多くの仕事です。
- ストレージとしてのsrc /メイン/リソース/静的なCSS / JS / htmlのディレクトリおよびその他の静的リソース
- SpringMVCコンポーネントのような構成ViewResolver /コンバータ/ HttpMessageConverterを望ま。
- デフォルトの設定は、Tomcatなどの組み込みWebコンテナ、デフォルトポート8080を使用しています。
その後、我々は直接OKで、Web開発関連の機能で、彼らはすべての基本的なを持って、依存関係を追加します。
全体的なのpom.xmlを見てください:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.forwhatsogood</groupId>
<artifactId>spring-boot-helloweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-helloweb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 添加web开发功能 -->
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
書き込みコントローラクラス
添加することにより、コントローラ@RestController
注釈は、コントローラは、JSONに設定された値を返します。
package com.forwhatsogood.springboothelloweb;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//注册为控制器,可响应web请求,同时将方法返回值序列化为json
@RequestMapping("/hello")//访问路径
public class HelloController {
@RequestMapping("web")
public Map web() {
Map<String,String> map=new HashMap<String,String>();
map.put("path","/hello/web");
return map;
}
}
このとき、プロジェクト開始SpringBootHellowebApplication実行は直接アクセスしhttp://localhost:8080/hello/web
、以下の結果:
リクエストに見える成功応答を、そして値のJSON形式を返します。
スキャン経路を設定します
コントロールクラスとクラスがスタートパケットでない場合は、手動でのパケットの必要性は、スキャンを指定し、指定されたパケットの使用に注意を払う必要があり、デフォルトの手動スキャンはありません、手動でする必要が一つずつ指定してください。
package com.forwhatsogood.springboothelloweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = { "com.forwhatsogood.springboothelloweb", "com.forwhatsogood.other" }) // 手动指定扫描的包
public class SpringBootHellowebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHellowebApplication.class, args);
}
}
@RestControllerを使用した場合はいないが、あなたはメソッドを追加する必要があり@ResponseBody
、次のように、JSON形式にシーケンスの値を返すようにします:
package com.forwhatsogood.other;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller//注册为控制器,可响应web请求
@RequestMapping("/user")//访问路径
public class UserController {
@RequestMapping("web")
@ResponseBody
public Map web() {
Map<String,String> map=new HashMap<String,String>();
map.put("path","/user/web");
return map;
}
}
テスト、訪問の住所:http://127.0.0.1:8080/hello/web
、http://127.0.0.1:8080/user/web
すべて成功した訪問!
コンフィギュレーションアクセスポートとパス
もっと簡単な、application.properties
の直接添加:
# 配置端口
server.port=8000
# 配置路径 注意springboot2.0之前是server.context-path=/demo
server.servlet.context-path=/demo
プログラムを再起動し、アクセスアドレスは次のようになります。
http://127.0.0.1:8000/demo/hello/web
http://127.0.0.1:8000/demo/user/web
概要
使用後SpringBoot、導入のみ各モジュールPOM、およびBeanコンポーネントの、直接書き込みコントローラと、自動的SpringBootが導入された認識は、直接使用することができます。
非常にシンプルで便利!