Spring Boot探路者之helloworld
来源:互联网 发布:阿里云ddos 编辑:程序博客网 时间:2024/04/29 15:29
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
简单的说将java的各大流行框架通过页面配置整合起来,省去了项目初建花费大量时间去组装调试的过程,从而简化了项目框架搭建流程。内置了tomcat,jetty等容器,无需再通过tomcat部署war包的方式部署项目。目前在微服务架构中应用很广泛。
下面就简单介绍下使用流程:
1.配置下载项目
访问网站http://start.spring.io/,根据自己的情况选择maven或gradle和spring boot版本
点击“Switch to the full version”配置自己项目说明
在创建项目的下方可以根据自己的需求选择合适框架依赖。
选择好后点击“Generate Project”下载项目工程。
也可以通过安装ide插件支持,如eclipse安装spring tools tips,新建spring starter project项目
2.项目结构
和maven项目一样这里就不多少了,有一个*Application类的主程序,运行即可启动程序。
@SpringBootApplicationpublic class HelloworldApplication{ public static void main(String[] args) { SpringApplication.run(HelloworldApplication.class, args); }}
3.编写demo代码
1.Controller编写
@RestController@RequestMapping("/user")public class UserController { @Autowired private UserServie userServie; @RequestMapping(value="/sayHello/{id}",method = RequestMethod.GET) public String sayHello(@PathVariable Long id){ return userServie.sayHello(id); } @RequestMapping(value="",method = RequestMethod.POST) public Long save(@RequestBody User user){ return userServie.addUser(user); } @RequestMapping(value="/list",method = RequestMethod.GET) public List<User> list(){ return userServie.findList(); }}
spring boot默认的包扫描是从Application同级包路径自上而下扫描注入。
2.单元测试
使用RestTemplate进行Controller接口测试,
添加测试包依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)public class HelloworldApplicationTests { @Autowired private TestRestTemplate restTemplate; @Test public void testSayHello() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setContentType(MediaType.APPLICATION_JSON); requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); User user = new User(); user.setAge(30); user.setBirthday(new Date()); user.setName("tom"); HttpEntity<User> request = new HttpEntity<User>(user, requestHeaders); Long id = restTemplate.postForObject("/user", request, Long.class); Assert.assertEquals(restTemplate.getForEntity("/user/sayHello/{id}", String.class, id).getBody(), "hello tom");; } @Test public void testSayHelloNoFound() throws Exception { Assert.assertEquals(restTemplate.getForEntity("/user/sayHello/{id}", String.class, -1).getBody(), "not found");; } @Test public void testsave() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setContentType(MediaType.APPLICATION_JSON); requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); User user = new User(); user.setAge(30); user.setBirthday(new Date()); user.setName("jetty"); HttpEntity<User> request = new HttpEntity<User>(user, requestHeaders); Assert.assertNotNull(restTemplate.postForEntity("/user", request, Long.class).getBody()); } @Test public void testList() throws Exception { Assert.assertTrue(restTemplate.getForEntity("/user/list", List.class).getBody().size() > 0); }}
TestRestTemplate是对RestTemplate进行了包装.
3.server启动配置
1)配置文件配置,如:application.properties
server.port=8000server.context-path=/helloworld
可以安装spring tools tips插件,配置参数可以提示
2)实现接口EmbeddedServletContainerCustomizer
@Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8000); container.setContextPath("/helloworld"); }
3)命令行启动参数配置
java -jar -Dserver.port=8000 helloworld.jar
4.部署方式
1.集成tomcat或jetty通过命令启动
spring boot默认集成了tomcat,如果使用jetty修改pom,排除tomcat依赖,增加jetty依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
在使用maven打成jar使用java命令启动
2.部署到JavaEE容器
1)改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
public class HelloworldApplication extends SpringBootServletInitializer{ private static final Logger logger = LoggerFactory.getLogger(SpringBootSampleApplication.class); @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(this.getClass()); }}
修改pom文件中jar 为 war
<packaging>war</packaging>
修改pom,排除tomcat插件,同上。打包部署到容器启动即可。
5.开发调试
springBoot对调试支持很好,使用热启动,修改之后可以实时生效,需要修改以下的配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>...<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins>
该模块在完整的打包环境下运行的时候会被禁用。
6.使用Linux服务的方式启动、停止、重启
1.首先在 pom.xml 中配置插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build>
2.上传jar包到服务器,假设部署路径为 /var/apps/myapp.jar ,使用命令做一个软连接到 /etc/init.d 目录,命令:
ln -s /var/apps/myapp.jar /etc/init.d/myapp
3.给jar文件授予可执行权限,命令:
chmod +x myapp.jar
4.我们可以使用service myapp start|stop|restart 来对应用进行启停了
执行命令后将得到形如 Started|Stopped [PID] 的结果反馈。
默认PID文件路径:/var/run/appname/appname.pid
默认日志文件路径:/var/log/appname.log
实
5.使用自定义的.conf文件来变更默认配置,方法如下:
在jar包相同路径下创建一个.conf文件,名称应该与.jar的名称相同,如myapp.conf,其内容配置可以如下:
JAVA_HOME=/usr/local/jdkJAVA_OPTS=-Xmx1024MLOG_FOLDER=/data/logs/myapp
实例源码:https://github.com/slimina/springboot-study
- Spring Boot探路者之helloworld
- Spring Boot探路者之应用配置
- Spring Boot之HelloWorld
- spring-boot之helloworld
- Spring Boot 之 HelloWorld详解
- (一) spring boot 之 helloworld
- Spring Boot 之 HelloWorld详解
- spring boot初识之helloworld
- Spring Boot入门之 HelloWorld
- Spring Boot 之 HelloWorld详解
- spring boot之HelloWorld详解
- Spring Boot 之 HelloWorld详解
- 001-spring boot 之helloworld
- Spring Boot 基础篇之 HelloWorld
- Spring Boot之HelloWorld环境搭建!!!
- Spring Boot入门helloworld
- Spring boot 入门helloWorld
- spring boot 一 HelloWorld
- JAVA匿名内部类
- linux操作学习06文件权限:文件属性查看(ls),文件所有人、组的管理(chown、chgrp),文件普通权限(chmod)、系统默认权限的设定(umask)、文件特殊权限
- Android Studio 设置注释模板
- 转载--HTML5中的Scoped属性使用实例
- 【matlab】GUI的菜单和工具栏常用操作_超级好用!!
- Spring Boot探路者之helloworld
- C/C++中static 关键字的总结
- mybatis里oracle与MySQL的insert_update
- ADV-104-算法提高-打水问题
- 40.服务器搭建准备
- 蓝桥杯练习系统_基础练习_十六进制转八进制
- MPI学习笔记之并行程序概述
- java 创建 不可以滚动的 表格
- golang(5)使用beego 开发 api server 和前端同学拆分开发,使用swagger