SpringBoot第一个工程创建(开发工具Idea)

使用Idea来构建我们第一个Springboot工程

Idea版本:IntelliJ IDEA 2019.2.4 (Community Edition)

这里我们有两种创建Spring Boot项目的方法。

一、在线创建项目
访问Spring Boot的官方网站,在线创建项目然后本地导入我们的IDEA中就可以了。
1、访问网址:https://start.spring.io/
根据提示配置项目信息,配置好之后点击Generate Project按钮生成项目。

2、将下载的项目解压后导入到IDEA中即可。
点击IDEA File --> New --> Project from Existing Sources… => 选择你解压后的项目文件夹目录 --> 点击Ok --> 选择Import project from external model --> 选中Maven --> 一路点击Next直到项目导入成功。

二、在Idea工具中创建项目(推荐)
利用IDEA工具里面的Spring Assistant创建并初始化一个项目(推荐这种方式来创建项目)
1、File --> New --> Project… ,选择Spring Assistant。
在这里插入图片描述
========遇到没有Spring Assistant问题?
File --> Settings… --> Plugins 可以找到,安装这个插件,重启idea就可以
实测OK
《截图说明下》
========问题已解决

2、根据工具提示一步步创建工程。
在这里插入图片描述

3、maven更新依赖

这个时候还没有配置maven,可能会遇到自动更新出问题的情况。
在这里插入图片描述
如果这个目下没有这个文件,就使用everything搜索一下,copy一个过来配置。配置完成重启下idea就可以正常更新依赖文件了。

我这边遇到一个“Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:2.2.6.RELEASE”问题。
这个是由于在默认的maven仓库找不到这个依赖,我们添加aliyun的maven库。
========如何添加aliyun的maven仓库?
参考地址:https://www.cnblogs.com/belibai/p/11370720.html

先找到我们工程使用的Maven(有可能配置了多个Maven)
进入Settings --> Maven ,选择默认的Bundled (Maven 3)。这个时候就需要找到你的Manen settings.xml文件的路径了(工具上有提示)。

找到mirrors,添加以下内容

<mirror>  
      <id>alimaven</id>  
      <name>aliyun maven</name>  
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>          
</mirror>

这里是引用

注意这个是配置本地的仓库的。(下载下来的依赖文件就会放在这个目录下)
D:/MavenRepository/repo

修改完成了配置,idea提示修改,确认后,再右击工程 --> Maven --> Reimport 就可以了。
========问题已解决

实测OK,没有报错了。

三、创建一个最简单的Controller,浏览器中访问测试
在包名下创建一个目录 controller,再创建一个HelloController.java

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public String hello(){
        return "hello springboot";
    }
}
注意:
1、我们浏览器中的url使用的是value = "/hello" ,方法名称不要求和这个一样,但为了规范还是一样比较好。
2、@ResponseBody这个必须加上

========遇到一个启动失败的问题
报错信息如下:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
//无法配置数据库,没有指定url属性,并且无法配置embedded datasource
Reason: Failed to determine a suitable driver class
//原因:无法明确指定正确的驱动类(driver.class)
 
Action:
 
Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

解决方法:

程序入口处:
 
@SpringBootApplication
public class DemoApplication {
 
修改为:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {
}

在这里插入图片描述

========问题已解决

浏览输入:http://localhost:8080/hello 页面就会正常返回我们这个 hello springboot 了。

在这里插入图片描述

总结:使用Springboot我们都不用部署tomcat,很方便。

四、添加swagger
swagger就是一款让你更好的书写API文档的框架,而且swagger可以完全模拟http请求,入参出参和实际情况差别几乎为零。
添加的步骤:
1、配置pom.xml
在dependencys标签中添加

	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.5.0</version>
	</dependency>
	<!-- swagger-ui -->
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.5.0</version>
	</dependency>

同步下就可以下载对应的依赖了。

2、配置config
在代码中新建一个config包,然后再创建SwaggerConfig类

	package com.example.demo.config;

	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;

	import io.swagger.annotations.ApiOperation;
	import springfox.documentation.builders.ApiInfoBuilder;
	import springfox.documentation.builders.RequestHandlerSelectors;
	import springfox.documentation.service.ApiInfo;
	import springfox.documentation.spi.DocumentationType;
	import springfox.documentation.spring.web.plugins.Docket;
	import springfox.documentation.swagger2.annotations.EnableSwagger2;

	@Configuration
	@EnableSwagger2
	public class SwaggerConfig {

		@Bean
		public Docket swaggerSpringMvcPlugin() {
			return new Docket(DocumentationType.SWAGGER_2)
					.apiInfo(apiInfo())//在swagger页面显示的title
					.select()
					.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
					.build();
		}

		/**
		 * 在swagger页面显示的title
		 * @return
		 */
		private ApiInfo apiInfo() {
			return new ApiInfoBuilder()
					.title("Demo 接口Api")
					.version("1.0")
					.build();
		}

	}

3、配置controller

	package com.example.demo.controller;

	import io.swagger.annotations.Api;
	import io.swagger.annotations.ApiOperation;
	import org.springframework.stereotype.Controller;
	import org.springframework.web.bind.annotation.RequestMapping;
	import org.springframework.web.bind.annotation.RequestMethod;
	import org.springframework.web.bind.annotation.ResponseBody;

	@Api(value = "测试Demo接口")//swagger配置
	@Controller
	public class HelloController {
		@ApiOperation(notes = "输出hello springboot文本", value = "测试接口", httpMethod = "GET")//swagger配置
		@RequestMapping(value = "/hello", method = RequestMethod.GET )//produces = "application/json"输出的json字符串
		@ResponseBody
		public String hello(){
			System.out.println("hello springboot");
			return "hello springboot";
		}
	}
主要就配置1个地方:@ApiOperation

4、测试:
浏览器打开:http://localhost:8080/swagger-ui.html
《截图说明》

实际测试OK

========遇到个问题:
在idea中运行会报代码错误。但是使用mvn clean package 打包运行是正常的。(说明代码没问题)

Error:(4, 30) java: 程序包io.swagger.annotations不存在

经过仔细排查,可能问题在于之前的maven的settings.xml的配置哪里不对。如果通过everything搜索下这个jar依赖,我们指定的D:/MavenRepository/repo仓库中有,而这个默认的C:\Users…m2\repository中没有。

再看我们Idea的maven配置,发现并没有勾选overwrite。
在这里插入图片描述
这个localRepository本地仓库指定并没有生效。还是找到默认的Bunde(Maven3)里面的配置文件(这个里面是没有配置localRepository的)指向的是C:\Users…m2\repository这个仓库。而这个仓库刚好就没有这个包。所以报错了。

解决方法:
删除C:\Users…m2\settings.xml配置文件,从网上下载个maven(下载网址http://maven.apache.org/download.cgi)
在这里插入图片描述

配置个环境变量(mvn --version)看看是否生效
在这里插入图片描述
在这里插入图片描述
========问题解决

========遇到一个启动swagger时,浏览器弹框报错问题

Unable to infer base url.
This is common when using dynamic servlet registration or when the API is behind an API Gateway.
The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/.
Please enter the location manually:

替换版本解决(从2.5.0替换为2.9.2)

		<!--添加swagger-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<!-- swagger-ui -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>

========问题解决

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章