SpringBoot basic introduction to interface development and the Get method

Brief introduction

SpringBoot possess the characteristics are:

(1) can be created independently of Spring application, and based on their Gradle or Maven plug can create executable JARs and WARs;

(2) Embedded like Tomcat or Jetty Servlet container;

(3) providing automatic configuration of "starter" Project Object Model (of POMS) to simplify Maven configuration;

(4) auto-configuration Spring container as possible;

(5) providing a ready properties, such as indexes of health checks and external configuration;

(6) absolutely no code generation, XML configuration is not required

Official website address:  https://spring.io/projects/spring-boot/

Rapid interface development

1, maven configuration information

<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, simple case

package com.hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

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

3, using the Get method of developing interfaces SpringBoot

3.1, create SpringBoot import category

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@ComponentScan("com")
public class Application {

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

3.2, a simple interface to get method

package com.server;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyGetMethod {

    @RequestMapping(value = "/getCookie",method = RequestMethod.GET)
    public String getCookie(){

        return "这是一个不带参数的Get请求";
    }
}

3.3, Cookie obtain information get an interface method

package com.server;

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.HttpServletResponse;

@RestController
public class MyGetCookieMethod {

    @RequestMapping(value = "/getCookieMethod",method = RequestMethod.GET)
    public String getCookieMethod(HttpServletResponse response){

        Cookie cookie = new Cookie("login","true");
        response.addCookie(cookie);
        return  "这是一个获取Cookie信息的Get方法接口";
    }
}

 

3.3, need to carry the Get method Cookies information access interface

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 method requires a request with parameters:

/**
 * 带参数的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 "恭喜登录成功了";
}

 

 

Published 17 original articles · won praise 0 · Views 172

Guess you like

Origin blog.csdn.net/qq_37637691/article/details/90437145