文章目录
pom
- pom
- application.yml
- 启动类
- 统一异常处理类
创建tensquare_search模块 pom文件内容如下
tensquare_parent
com.tensquare
1.0-SNAPSHOT
4.0.0
tensquare_search
com.tensquare
tensquare_common
1.0-SNAPSHOT
org.springframework.data
spring-data-elasticsearch
3.0.6.RELEASE
application.yml
server:
port: 9007
spring:
application:
name: tensquare-search #指定服务名
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300
启动类
在com.tensquare.search.SearchApplicaion 编写启动类
package com.tensquare.search;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
/**
* 类名称:RecruitApplicaion
* 类描述:TODO
*
* @author: taohongchao
* 创建时间:2019/1/20 14:56
* Version 1.0
*/
@SpringBootApplication
public class SearchApplicaion {
public static void main(String[] args){
SpringApplication.run(SearchApplicaion.class);
}
@Bean
public IdWorker idWorker() {
return new IdWorker(1,1);
}
}
统一异常处理类
在com.tensquare.search.controller包下创建统一的异常处理类.
package com.tensquare.search.controller;
import entity.Result;
import entity.StatusCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 统一异常处理类
*/
@ControllerAdvice
public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result error(Exception e){
e.printStackTrace();
return new Result(false, StatusCode.ERROR, "执行出错");
}
}