SpringBoot基本介绍及Get方法接口开发

简介

SpringBoot所具备的特征有:

(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;

(2)内嵌Tomcat或Jetty等Servlet容器;

(3)提供自动配置的“starter”项目对象模型(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、简单案例

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、使用SpringBoot开发Get方法的接口

3.1、创建SpringBoot入口类

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、简单的get方法接口

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信息的get方法接口

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、需要携带Cookies信息访问的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
今日推荐