您当前的位置: 首页 > 

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

黑马十次方项目day04-09之文章模块的创建

java持续实践 发布时间:2019-01-20 15:00:58 ,浏览量:2

文章目录
      • pom
      • application.yml
      • 启动类
      • 统一异常处理类

pom

创建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, "执行出错");
    }
}

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0407s