JavaWeb核心代码

文章目录

1、全局异常处理
2、评论功能实现
3、抢优惠券代码
3、hase 服务
4、权限拦截
创建商户
消费kafka 核心代码
1、全局异常处理

public class GlobalExceptionHandler {

@ResponseBody
@ExceptionHandler(value = Exception.class)
public ErrorInfo<String> errorHandler(HttpServletRequest request, Exception ex) throws Exception {

    ErrorInfo<String> info = new ErrorInfo<String>();

    info.setCode(ErrorInfo.ERROR);
    info.setMessage(ex.getMessage());
    info.setData("Do Not Have Return Data");
    info.setUrl(request.getRequestURL().toString());

    return info;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2、评论功能实现

public Response createFeedback(Feedback feedback) {

    if (!feedback.validate()) {
        log.error("Feedback Error: {}", JSON.toJSONString(feedback));
        return Response.failure("Feedback Error");
    }

    Put put = new Put(Bytes.toBytes(RowKeyGenUtil.genFeedbackRowKey(feedback)));

    put.addColumn(
            Bytes.toBytes(Constants.Feedback.FAMILY_I),
            Bytes.toBytes(Constants.Feedback.USER_ID),
            Bytes.toBytes(feedback.getUserId())
    );
    put.addColumn(
            Bytes.toBytes(Constants.Feedback.FAMILY_I),
            Bytes.toBytes(Constants.Feedback.TYPE),
            Bytes.toBytes(feedback.getType())
    );
    put.addColumn(
            Bytes.toBytes(Constants.Feedback.FAMILY_I),
            Bytes.toBytes(Constants.Feedback.TEMPLATE_ID),
            Bytes.toBytes(feedback.getTemplateId())
    );
    put.addColumn(
            Bytes.toBytes(Constants.Feedback.FAMILY_I),
            Bytes.toBytes(Constants.Feedback.COMMENT),
            Bytes.toBytes(feedback.getComment())
    );

    hbaseTemplate.saveOrUpdate(Constants.Feedback.TABLE_NAME, put);

    return Response.success();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
3、抢优惠券代码

/**
*

给用户添加优惠券


* @param request {@link GainPassTemplateRequest}
* @param merchantsId 商户 id
* @param passTemplateId 优惠券 id
* @return true/false
* */
private boolean addPassForUser(GainPassTemplateRequest request,
Integer merchantsId, String passTemplateId) throws Exception {

    byte[] FAMILY_I = Constants.PassTable.FAMILY_I.getBytes();
    byte[] USER_ID = Constants.PassTable.USER_ID.getBytes();
    byte[] TEMPLATE_ID = Constants.PassTable.TEMPLATE_ID.getBytes();
    byte[] TOKEN = Constants.PassTable.TOKEN.getBytes();
    byte[] ASSIGNED_DATE = Constants.PassTable.ASSIGNED_DATE.getBytes();
    byte[] CON_DATE = Constants.PassTable.CON_DATE.getBytes();

    List<Mutation> datas = new ArrayList<>();
    Put put = new Put(Bytes.toBytes(RowKeyGenUtil.genPassRowKey(request)));
    put.addColumn(FAMILY_I, USER_ID, Bytes.toBytes(request.getUserId()));
    put.addColumn(FAMILY_I, TEMPLATE_ID, Bytes.toBytes(passTemplateId));

    if (request.getPassTemplate().getHasToken()) {
        String token = redisTemplate.opsForSet().pop(passTemplateId);
        if (null == token) {
            log.error("Token not exist: {}", passTemplateId);
            return false;
        }
        recordTokenToFile(merchantsId, passTemplateId, token);
        put.addColumn(FAMILY_I, TOKEN, Bytes.toBytes(token));
    } else {
        put.addColumn(FAMILY_I, TOKEN, Bytes.toBytes("-1"));
    }

    put.addColumn(FAMILY_I, ASSIGNED_DATE,
            Bytes.toBytes(DateFormatUtils.ISO_DATE_FORMAT.format(new Date())));
    put.addColumn(FAMILY_I, CON_DATE, Bytes.toBytes("-1"));

    datas.add(put);

    hbaseTemplate.saveOrUpdates(Constants.PassTable.TABLE_NAME, datas);

    return true;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
3、hase 服务

public boolean dropPassTemplateToHBase(PassTemplate passTemplate) {

    if (null == passTemplate) {
        return false;
    }

    String rowKey = RowKeyGenUtil.genPassTemplateRowKey(passTemplate);

    try {
        if (hbaseTemplate.getConnection().getTable(TableName.valueOf(Constants.PassTemplateTable.TABLE_NAME))
                .exists(new Get(Bytes.toBytes(rowKey)))) {
            log.warn("RowKey {} is already exist!", rowKey);
            return false;
        }
    } catch (Exception ex) {
        log.error("DropPassTemplateToHBase Error: {}", ex.getMessage());
        return false;
    }

    Put put = new Put(Bytes.toBytes(rowKey));

    put.addColumn(
            Bytes.toBytes(Constants.PassTemplateTable.FAMILY_B),
            Bytes.toBytes(Constants.PassTemplateTable.ID),
            Bytes.toBytes(passTemplate.getId())
    );

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
4、权限拦截

public boolean preHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o) throws Exception {

    String token = httpServletRequest.getHeader(Constants.TOKEN_STRING);

    if (StringUtils.isEmpty(token)) {
        throw new Exception("Header 中缺少 " + Constants.TOKEN_STRING + "!");
    }

    if (!token.equals(Constants.TOKEN)) {
        throw new Exception("Header 中 " + Constants.TOKEN_STRING + "错误!");
    }

    AccessContext.setToken(token);

    return true;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
创建商户

@Override
@Transactional
public Response createMerchants(CreateMerchantsRequest request) {

    Response response = new Response();
    CreateMerchantsResponse merchantsResponse = new CreateMerchantsResponse();

    ErrorCode errorCode = request.validate(merchantsDao);
    if (errorCode != ErrorCode.SUCCESS) {
        merchantsResponse.setId(-1);
        response.setErrorCode(errorCode.getCode());
        response.setErrorMsg(errorCode.getDesc());
    } else {
        merchantsResponse.setId(merchantsDao.save(request.toMerchants()).getId());
    }

    response.setData(merchantsResponse);

    return response;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
消费kafka 核心代码

public void receive(@Payload String passTemplate,
@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {

    log.info("Consumer Receive PassTemplate: {}", passTemplate);

    PassTemplate pt;

    try {
        pt = JSON.parseObject(passTemplate, PassTemplate.class);
    } catch (Exception ex) {
        log.error("Parse PassTemplate Error: {}", ex.getMessage());
        return;
    }

    log.info("DropPassTemplateToHBase: {}", passService.dropPassTemplateToHBase(pt));
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值