インターフェースの開発とGetメソッドの基本的な導入をSpringBoot

簡単な紹介

SpringBootが特徴である持っています:

(1)独立して作成することができる春のアプリケーション、および実行可能JARおよび戦争を作成することができ、それらのGradleやMavenプラグに基づきます。

(2)TomcatやJettyのサーブレットコンテナのように組み込み、

(3)簡単にするために(POMSの)「スターター」プロジェクトオブジェクトモデルの自動構成を提供するMavenの構成を、

可能な限り(4)自動設定Springコンテナ。

(5)このような健康診断および外部構成のインデックスとして準備特性を提供します。

(6)全くコード生成、XMLコンフィギュレーションは必要ありませんされていません

公式ウェブサイトのアドレス:  https://spring.io/projects/spring-boot/

迅速なインタフェースの開発

1、Mavenの設定情報

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>
 <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web-->
<dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2、単純なケース

パッケージcom.hello。

輸入org.springframework.boot *。; 
輸入org.springframework.boot.autoconfigure *。; 
輸入org.springframework.stereotype *。; 
輸入org.springframework.web.bind.annotation *。; 

@Controller 
@EnableAutoConfiguration 
パブリッククラスSampleController { 

    @RequestMapping( "/")
    @ResponseBody 
    文字列の自宅(){ 
        リターンの"Hello World!"; 
    } 

    公共の静的な無効メイン(文字列[]引数)は例外{スロー
        SpringApplication.run(SampleController.class、引数)。
    } 
}

図3に示すように、インターフェイスを開発するGETメソッドを使用SpringBoot

3.1、SpringBootインポートカテゴリを作成

輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.context.annotation.ComponentScan; 


@SpringBootApplication 
@ComponentScan( "COM")
パブリッククラスのアプリケーション{ 

    パブリック静的無効メイン(文字列[] args){ 
        SpringApplication.run(Application.class、引数)。
    } 
}

3.2、メソッドを取得するためのシンプルなインターフェイス

パッケージcom.server。

輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RequestMethod; 
輸入org.springframework.web.bind.annotation.RestController; 

@RestController 
パブリッククラスMyGetMethod { 

    @RequestMapping(値= "/ getCookie"、メソッド= RequestMethod.GET)
    パブリック文字列getCookie(){ 

        リターン"这是一个不带参数的取得请求"。
    } 
}

3.3、クッキーは、インタフェースメソッドを取得する情報を取得します

パッケージcom.server。

輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RequestMethod; 
輸入org.springframework.web.bind.annotation.RestController; 

インポートのjavax.servlet.http.Cookie。
インポートのjavax.servlet.http.HttpServletResponse; 

@RestController 
パブリッククラスMyGetCookieMethod { 

    @RequestMapping(値= "/ getCookieMethod"、メソッド= RequestMethod.GET)
    パブリック文字列getCookieMethod(HttpServletResponseの応答){ 

        クッキークッキー=新しいクッキー( "ログイン"、 "真")。
        response.addCookie(クッキー); 
        リターン"这是一个获取クッキー信息的取得方法接口"。
    } 
}

 

3.3、Getメソッドクッキー情報アクセスインタフェースを運ぶために必要

package hello;

import com.sun.deploy.net.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@RestController
public class getWithCookies {

    @RequestMapping(value = "/getWithCookie",method = RequestMethod.GET)
    public String getWithCookiesMethod(HttpServletRequest request){
        Cookie [] cookies = request.getCookies();
        for(Cookie cookie: cookies){
            if(cookie.getName().equals("login")&& cookie.getValue().equals("true")){
                return "恭喜登录成功";
            }
        }
        return "您必须,携带Cookies信息才能登录";
    }
}

図4は、Getメソッドは、パラメータを持つリクエストが必要です。

/**
 * 带参数的get请求方法1
 * */
@RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
public String getWithParam(@RequestParam String start,
                           @RequestParam String end){
    return "恭喜登录成功";
}

/**
 * 带参数的get请求方法二
 * */
@RequestMapping(value = "/get/with/param/{start}/{end}",method = RequestMethod.GET)
public String getWithParamMethod2(@PathVariable Integer start,
                                  @PathVariable Integer end){
    return "恭喜登录成功了";
}

 

 

公開された17元の記事 ウォンの賞賛0 ビュー172

おすすめ

転載: blog.csdn.net/qq_37637691/article/details/90437145