您当前的位置: 首页 >  微服务

java持续实践

暂无认证

  • 1浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

黑马十次方项目day03-15之创建吐槽微服务

java持续实践 发布时间:2019-01-19 15:17:26 ,浏览量:1

文章目录
      • 创建吐槽微服务
      • pom
      • yml
      • 主启动类
      • 统一异常处理类

创建吐槽微服务

由于代码生成器是基于springdatajpa的,而吐槽微服务使用的是MongoDB数据库. 因此吐槽微服务是不能用代码生成器的.得手动创建工程

在idea中,新创建一个模块,取名为tensquare_spit

pom

在pom文件中引入如下的依赖

   
        
            com.tensquare
            tensquare_common
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
    

yml

在resource目录下创建application.yml,填写的内容如下

server: 
  port: 9006
spring: 
  application:  
    name: tensquare-spit #指定服务名
  data:
    mongodb:
      host: 115.136 #mongo服务器的地址,用默认的端口,如果修改了端口,那么要在后面写端口
      database: spitdb  # MongoDB的数据库
主启动类

在com.tensquare.spit.SpitApplication包路径下,编写如下的主启动类

package com.tensquare.spit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import util.IdWorker;

/**
 * 类名称:SpitApplication
 * 类描述:吐槽微服务的启动类
 *
 * @author: taohongchao
 * 创建时间:2019/1/19 15:11
 * Version 1.0
 */


@SpringBootApplication
public class SpitApplication {
    /**
     *  主启动类
     * @param args
     */
    public static void main(String[] args){
        SpringApplication.run(SpitApplication.class);
    }

    //分布式id生成器
    @Bean
    public IdWorker idWorker() {
        return new IdWorker();
    }

}

统一异常处理类

在com.tensquare.spit.controller包下创建统一的异常处理类.

package com.tensquare.spit.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.0639s