使用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按钮下载项目压缩包

使用IntelliJ IDEA 开发工具搭建spring-boot项目笔记(一)_第1张图片

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

使用IntelliJ IDEA 开发工具搭建spring-boot项目笔记(一)_第2张图片

使用IntelliJ IDEA 开发工具搭建spring-boot项目笔记(一)_第3张图片

(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);
	}
}


	4.0.0

	com.example
	demo
	0.0.1-SNAPSHOT
	jar

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.5.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	
	

(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' 启动项目

使用IntelliJ IDEA 开发工具搭建spring-boot项目笔记(一)_第4张图片

项目启动成功的日志信息如下,下面那个图案是属于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> 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,调用过程截图如下

使用IntelliJ IDEA 开发工具搭建spring-boot项目笔记(一)_第5张图片

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

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

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

黄朝兵的达人课

轻轻松松学习SpringBoot2

你可能感兴趣的:(spring-boot,Java)