使用IntelliJ IDEA 开发工具搭建spring-boot项目笔记(一)

   (一)  写在前面

  •        McroService微服务可以说是当前互联网公司最火的技术了,笔者最近也开始一边学习spring-boot和spring-cloud技术,一边在家搭建Spring-boot项目。这里我选择用intelliJ IDEA开发工具是有一定原因的,笔者最近两年在公司开发使用的java开发工具一直是eclipse,前端用的是visual studio。但是我发现用eclipse搭建的spring-boot项目,不配置应用上下文application-context.xml启动不来,而且没有日志,博客上参考了不少解决方案还是没有解决。而要在spring-boot项目根目录下运行maven指令: mvn spring-boot:run 才能运行起来。看了很多搭建spring-boot项目采用的都是intelliJ IDEA, 于是也转而采用intelliJ IDEA,没想到很顺利就把自己的spring-boot的demo项目启动起来了。废话不再多说,下面贴上自己的项目搭建步骤截图和代码

  (二)搭建和启动步骤并开发简单的Restful风格接口

    (1) 首先,我们访问:http://start.spring.io/,选择构建工具 Maven,采用编程语言 Java,输入如图中的信息,我使用的spring-boot是2.0.5  版本,在Search for dependencies下的搜索输入框中输入Web并作为选中的依赖, 点击下面的Generate Project按钮下载项目压缩包

(2)解压项目后使用intelliJ IDEA开发工具导入到工作目录,导入过程截图如下:选择File-->New-->Project from Existing Source 从解压后的项目中导入spring-boot-demo项目,选择Maven Project file后点击OK按钮即可

(3)启动类DemoApplication.java与pom.xml为spring-io生成项目时自动生成,代码如下

ackage com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	
</project>

(4)新建一个TestController类,并写两简单的restful风格的接口,代码如下

package com.example.demo.com.example.demo.controller;

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

@RestController
@RequestMapping(value="/restful")
public class TestController {
    @RequestMapping(value="/test",method= RequestMethod.GET)
    public String testRestInterface(@RequestParam("name") String name){
        return "Hello:" + name;
    }

    @RequestMapping(value="/index",produces = "text/plain;charset=utf-8")
    public String index(){
        return "Hello Spring Boot!";
    }
}

(5) 点击Run菜单下的Debug 'DemoApplication' 启动项目

项目启动成功的日志信息如下,下面那个图案是属于spring-boot项目启动时特有的

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)

2018-09-22 00:54:30.747  INFO 10808 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on heshengfu1211 with PID 10808 (D:\myproject\demo\target\classes started by HP in D:\myproject\demo)
2018-09-22 00:54:30.750  INFO 10808 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-09-22 00:54:30.803  INFO 10808 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@45ac5f9b: startup date [Sat Sep 22 00:54:30 GMT+08:00 2018]; root of context hierarchy
2018-09-22 00:54:32.036  INFO 10808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-09-22 00:54:32.067  INFO 10808 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-09-22 00:54:32.068  INFO 10808 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-09-22 00:54:32.074  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : Loaded APR based Apache Tomcat Native library [1.2.17] using APR version [1.6.3].
2018-09-22 00:54:32.074  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2018-09-22 00:54:32.074  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2018-09-22 00:54:33.138  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.0.2o  27 Mar 2018]
2018-09-22 00:54:33.271  INFO 10808 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-09-22 00:54:33.271  INFO 10808 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2472 ms
2018-09-22 00:54:33.356  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-22 00:54:33.545  INFO 10808 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:33.808  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@45ac5f9b: startup date [Sat Sep 22 00:54:30 GMT+08:00 2018]; root of context hierarchy
2018-09-22 00:54:33.873  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/restful/index],produces=[text/plain;charset=utf-8]}" onto public java.lang.String com.example.demo.com.example.demo.controller.TestController.index()
2018-09-22 00:54:33.876  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/restful/test],methods=[GET]}" onto public java.lang.String com.example.demo.com.example.demo.controller.TestController.testRestInterface(java.lang.String)
2018-09-22 00:54:33.879  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-22 00:54:33.879  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-22 00:54:33.909  INFO 10808 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:33.909  INFO 10808 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:34.043  INFO 10808 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-09-22 00:54:34.087  INFO 10808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-22 00:54:34.090  INFO 10808 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.786 seconds (JVM running for 4.879)
(6)postman调用GET请求的接口,请求头Headers设置Content-Type:application/json,调用过程截图如下

响应状态Status=200, Body中输出了 Hello: Spring-boot表示接口调用成功

(7) chrom浏览器中输入http://localhost:8080/restful/index 在浏览器端调用另一个返回text/plain格式用于浏览器端显示的接口,截图如下,一样调用成功

本人后续还将继续更新自己的spring-boot demo项目,下一篇博客讲开始采用JPA数据源连接mysql数据库,实现通过调用GET和POST请求接口实现数据集库数据的查询、插入、更新和删除。本文参考链接如下:

黄朝兵的达人课

轻轻松松学习SpringBoot2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

heshengfu1211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值