【Java】【spring boot】springboot入门

quick start

进入
https://start.spring.io/
在依赖里 加一个springweb
版本不要选snapshot
Java选14 generate就会下载一个压缩包

解压 用idea打开
找到src/main/java/com/example/demo/DemoApplication.java
改成以下内容


              package com.example.demo;
              import org.springframework.boot.SpringApplication;
              import org.springframework.boot.autoconfigure.SpringBootApplication;
              import org.springframework.web.bind.annotation.GetMapping;
              import org.springframework.web.bind.annotation.RequestParam;
              import org.springframework.web.bind.annotation.RestController;
              
              @SpringBootApplication
              @RestController
              public class DemoApplication {
                
                  
                  public static void main(String[] args) {
                  SpringApplication.run(DemoApplication.class, args);
                  }
                  
                  @GetMapping("/hello")
                  public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
                  return String.format("Hello %s!", name);
                  }
                
              }
            

点build会报一个错 无效的发行版
感觉是我的java版本太新了 guide里面也说建议用Java8或者11
但是我是14
在这里插入图片描述
到这个里面把language level改成11好了

到项目路径运行指令

mvnw spring-boot:run

等了半天困得受不了 了
下来发现已经好了。
在这里插入图片描述

到浏览器试一下
好了
还可以带参数
在这里插入图片描述

building an application

需要Gradle 4+ or Maven 3.2+
但是我都没有 不知道行不行

重新起一个springboot项目
https://start.spring.io/
加入web依赖

创建一个web application
src/main/java/com/example/springboot/HelloController.java

package com.example.springboot;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

	@RequestMapping("/")
	public String index() {
		return "Greetings from Spring Boot!";
	}

}

create an application class
src/main/java/com/example/springboot/Application.java

package com.example.springboot;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

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

	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
		return args -> {

			System.out.println("Let's inspect the beans provided by Spring Boot:");

			String[] beanNames = ctx.getBeanDefinitionNames();
			Arrays.sort(beanNames);
			for (String beanName : beanNames) {
				System.out.println(beanName);
			}

		};
	}

}

运行

./mvnw spring-boot:run

让这个springboot项目保持运行,额外再起一个终端
在这里插入图片描述

添加单元测试
注意是在test文件夹
src/test/java/com/example/springboot/HelloControllerTest.java

package com.example.springboot;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

	@Autowired
	private MockMvc mvc;

	@Test
	public void getHello() throws Exception {
		mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
				.andExpect(status().isOk())
				.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
	}
}

也可以写一个简单的全栈继承测试
创建一个test
src/test/java/com/example/springboot/HelloControllerIT.java

package com.example.springboot;

import static org.assertj.core.api.Assertions.*;

import java.net.URL;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {

	@LocalServerPort
	private int port;

	private URL base;

	@Autowired
	private TestRestTemplate template;

    @BeforeEach
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
    }

    @Test
    public void getHello() throws Exception {
        ResponseEntity<String> response = template.getForEntity(base.toString(),
                String.class);
        assertThat(response.getBody()).isEqualTo("Greetings from Spring Boot!");
    }
}

服务器会随机开一个port

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是基于 Spring 框架的开源框架,它简化了 Spring 应用程序的开发过程,提供了快速构建应用程序的能力。本文将介绍 Spring Boot入门知识。 1. 环境准备 在开始使用 Spring Boot 之前,需要安装以下环境: - JDK 1.8 或更高版本 - Maven 或 Gradle 2. 创建 Spring Boot 项目 可以使用 Spring Initializr 快速创建 Spring Boot 项目。Spring Initializr 是一个基于 Web 的工具,可以用于生成 Spring Boot 项目的基础结构。在创建项目时,可以选择所需的依赖项和插件。 可以通过以下步骤创建 Spring Boot 项目: 1. 打开 https://start.spring.io/。 2. 选择项目的基础设置,如项目名称、描述、包名等。 3. 选择所需的依赖项和插件。 4. 点击 Generate 按钮生成项目。 3. 编写 Spring Boot 应用程序 可以使用 Spring Boot 快速构建应用程序。在应用程序中,可以使用注解来标记类和方法,从而告诉 Spring Boot 如何创建和管理它们。 以下是一个简单的 Spring Boot 应用程序示例: ``` @SpringBootApplication @RestController public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } @GetMapping("/") public String helloWorld() { return "Hello, World!"; } } ``` 在这个示例中,@SpringBootApplication 注解指示 Spring Boot 应用程序,并启用自动配置。@RestController 注解指示该类是一个控制器,并将其托管在 Spring 上下文中。@GetMapping 注解指示该方法处理 HTTP GET 请求,并返回一个字符串。 4. 运行 Spring Boot 应用程序 可以使用以下命令运行 Spring Boot 应用程序: ``` mvn spring-boot:run ``` 或者,可以将应用程序打包成可执行的 JAR 文件,并使用以下命令运行: ``` java -jar <jar-file-name>.jar ``` 5. 总结 本文介绍了 Spring Boot入门知识,包括环境准备、创建 Spring Boot 项目、编写应用程序和运行应用程序。Spring Boot 提供了快速构建应用程序的能力,可以大大简化开发过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值