Spring Boot(二):快速搭建web项目或微服务
来源:互联网 发布:急难先锋4优化 编辑:程序博客网 时间:2024/05/21 18:49
上一篇博客对spring boot的来世今生进行了介绍,这篇就带领大家快速的创建一个spring boot的web项目或者微服务。
一、.新建项目
1.方法一
(1)自己创建java工程
(2)新建spring boot的application
(3)新建pom并引入starter
二、方法二
1.通过官网自动生成微服务项目
这种方法简便,省去了我们创建文件所需的时间。会生成一个压缩包
2.由于刚生成的项目没有更新jar包等初始化的文件。不能采用import的形式导入,下面是导入方法
选择项目
直接下一步、下一步
3.官网生成的项目目录如下
(1)一个application文件(项目启动的入口)
(2)一个property文件,用来进行配置(自己可以换成yml文件,使用起来更方便一些)
(3)单元测试
4.pom文件,注释进行了说明
<!--sprong boot的默认的一些配置--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--jdk--> <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> <!--spring boots tarter项目启动会用到--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--单元测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--maven打包发布用,可以生成可执行的jar包--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
注意:
1)Spring Boot依赖的groupId为 org.springframework.boot 。 通常你的Maven POM文件需要继承 spring-boot-starter-parent ,
然后声明一个或多个“Starter POMs”依赖。 Spring Boot也提供了一个用于创建可执行jars的Maven插件。
2)spring-boot-starter-web 添加了Tomcat和Spring MVC
5.创建一个可访问的API
(1)新建一个controller
package com.example.demo.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController { @RequestMapping(value="/login",method= RequestMethod.GET) public String assetsSummary(){ return "恭喜,登录成功!"; }}
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.jsp页面的,则其显示success.返回json格式的数据
2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
(2)配置端口
(3)启动项目
启动成功
默认使用tomcat启动
(4)访问接口
至此,一个简单spring boot项目搭建成功。
- Spring Boot(二):快速搭建web项目或微服务
- Spring-boot:快速搭建微服务框架
- WEB开发----Spring Boot项目快速搭建
- 用Spring Boot & Cloud,Angular2快速搭建微服务web应用 - 实现RESTful CRUD
- 用Spring Boot & Cloud,Angular2快速搭建微服务web应用 - 增加代理服务器
- 用Spring Boot & Cloud,Angular2快速搭建微服务web应用 - AngularJS2客户端
- 用Spring Boot & Cloud,Angular2快速搭建微服务web应用 - 增加权限控制
- MicroService4Net快速搭建微服务(二)
- SPRING BOOT快速搭建WEB应用教程(二)—HELLOWORLD
- 快速搭建Spring Boot项目
- Spring Boot 项目快速搭建
- maven+spring boot搭建简单微服务
- 微服务之Spring Boot 简单搭建
- maven+spring boot搭建简单微服务
- spring boot 通过Eureka搭建微服务
- 快速搭建RESTful Web Service(Spring Boot)
- spring-boot入门(一)快速搭建一个web应用
- springcloud微服务一:spring boot基础项目搭建及问题处理
- Android利用android:indeterminateDrawable来实现ProgressBar三种方式
- H5模拟面试题
- MongoDB、Cassandra 和 HBase 三种 NoSQL 数据库比较
- 第8周项目1-建立顺序串的算法库
- Java 重写与重载的区别
- Spring Boot(二):快速搭建web项目或微服务
- ECMAScript 6 入门学习(6.函数的扩展)
- router1
- 多线程
- $.ajaxFileUpload is not a function
- charles ios https证书安装后不能抓包 unknown
- 微信小程序幻灯的swiper标签宽和高度自适应
- 【Leetcode】String to Integer (atoi)
- 第九周项目2