本章内容概述
1、STS简介
2、STS安装
3、Springboot整合ES
4、配置Maven镜像
5、Postman的介绍及安装
1、STS简介
如果你是一个喜欢用spring的人,你可能会在欣赏spring的强大功能外,对其各样的配置比较郁闷,尤其是相差较大的版本在配置文件
方面会存在差异,当然你可以去花不少的时间去网上查找相关的资料,当你准备使用更高版本spring的时候,但这无异会让人感觉烦恼。
spring其实也早就意识到了这一点,并针对此给出了很好的解决方法,那就是在基于Eclipse的基础上,spring提供了针对Spring开发的
Spring Tool Suite集成开发工具,通过该工具,可以很轻易地生成一个spring的工程,比如web工程,最令人兴奋的是工程里的配置文件都
会自动为你生成,你再也不用操心配置文件的格式及各种配置文件了。
简而言之: sts是一个定制版的Eclipse,专为Spring开发定制的,方便创建调试运行维护Spring应用。
2、STS安装
2.1 安装方式1
方法1.官网下载直接当作eclipse使用:https://spring.io/tools3/sts/all
选择最新版本3.9.8下载
下载完成之后解压缩直接进入运行sts.exe即可以打开含有sts的eclipse了,如下
执行STS.exe, 其实就是eclipse,所以第一次也会提示设置工作空间,自行设置即可。
STS界面如下(类同eclipse)。
2.2 安装方式2:
根据自己本地安装的eclipse下载sts,然后通过插件导入的方式,首先要确认自己eclipse的版本,如下:
a. Help --> About Eclipse
如上可以知道版本为4.4.2,或者如下点击左下角的图标查看:
b. 接着我们取官网下载对应的4.4.2版本的插件,切记版本要对应:https://spring.io/tools3/sts/all
注:最好使用稍微新一点版本的Eclipse,因为旧版本的eclipse不太好找对应的sts
c. help-->install new software
d.如下选择四个带有Spring IdE的选项
之后就一直是next按钮,然后就可以了。
e. windows-->preferences,出现如下就对了
3、Springboot整合ES
1、File-->New-->Spring Starter Project-->输入内容-->点击Next
2、选择要加载的依赖-->点击Finish
3、在pom.xml中加入依赖 [如果网速不好,最好配置镜像:见配置Maven镜像]
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot 整合es-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--
Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。例如开发中经常需要写的javabean,都需要花时间去添加相
应的getter/setter,也许还要去写构造器、equals等方法,而且需要维护,当属性多时会出现大量的getter/setter方法,这些显得很冗
长也没有太多技术含量,一旦修改属性,就容易出现忘记修改对应方法的失误。
Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。出现的神奇就是
在源码中没有getter和setter方法,但是在编译生成的字节码文件中有getter和setter方法。这样就省去了手动重建这些代码的麻烦,使
代码看起来更简洁些。
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
4、创建com.newcapec.entity包,然后新建实体类,对应elasticsearch的文档
PUT /newcapec/user/1
{
"name":"jingerbo",
"age" :28,
"sex" :1
}
package com.newcapec.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
//@Data注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
@Document(indexName = "newcapec",type = "user")
public class UserEntity {
//对应文档id
@Id
private String id;
private String name;
private Integer age;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "UserEntity [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
5、创建com.newcapec.dao包,然后创建UserDao接口,让这个接口继承CrudRepository<T, ID>
T 类型,也就是UserEntity
ID ID数据类型
然后不需要做其他事情,因为只要继承了CrudRepository,对于user类型的增删改查都已经封装好了
package com.newcapec.dao;
import org.springframework.data.repository.CrudRepository;
import com.newcapec.entity.UserEntity;
public interface UserDao extends CrudRepository<UserEntity, String>{
}
6、创建com.newcapec.controller包,然后定义UserController类,写增删改查
package com.newcapec.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.newcapec.dao.UserDao;
import com.newcapec.entity.UserEntity;
@RestController
public class UserController {
@Autowired
private UserDao userDao;
//添加文档
@RequestMapping("/addUser")
public UserEntity addUser(@RequestBody UserEntity user) {
System.out.println(user);
return userDao.save(user);
}
//根据id查询文档
@RequestMapping("/findUser")
public Optional<UserEntity> findUser(String id) {
return userDao.findById(id);
}
}
7、在src/main/resources添加application.yml配置文件
application.yml
spring:
data:
elasticsearch:
####集群名称
cluster-name: myes
####地址
cluster-nodes: 192.168.34.64:9300
8、启动项目
package com.newcapec;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.newcapec.dao")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
9、错误处理
not part of the cluster Cluster [myes], ignoring...
解决方案:
vim /usr/local/elasticsearch-6.4.3/config/elasticsearch.yml
cluster.name: myes
10、测试新增:打开Postman,按图操作
11、测试查询
4、配置Maven镜像
1、Window-->Preferences-->Maven-->User Settings-->装载Maven配置文件
2、打开Maven setting配置文件:配置本地仓库和镜像
5、Postman的介绍及安装
5.1 Postman背景介绍
用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比
如著名的Firebug等网页调试工具。今天给大家介绍的这款网页调试工具不仅可以调试简单的css、html、脚本等简单的网页基本信息,它还
可以发送几乎所有类型的HTTP请求!Postman在发送网络HTTP请求方面可以说是Chrome插件类产品中的代表产品之一。
5.2 Postman的操作环境
postman适用于不同的操作系统,Postman Mac、Windows X32、Windows X64、Linux系统,还支持postman 浏览器扩展程序、post
man chrome应用程序等。
5.3 Postman重要提示:
由于2018年初chrome停止对chrome应用程序的支持,你的postman插件可能无法正常使用了。目前chrome应用商店能使用的就是
chrome扩展程序和主题背景。
如果你还想用postman,那么可以使用下面官方推出的Postman 开发者版本。
5.4 Postman使用参考文档:
1.官方英文文档:https://www.getpostman.com/docs/v6/
2.chrome插件整理的postman中文使用教程:http://chromecj.com/web-development/2017-12/870.html
5.5 Postman下载安装
1、下载地址 https://www.getpostman.com/apps
2、Postman安装
Postman的安装非常简单,在windows系统只需要双击安装包,然后什么都不需要操作,它直接就自己完成了。
如下图:
3、创建帐号,略
4、设置工作区工具和布局