Spring4.0实战 rest相关

package com.paic.pay.merchant.web;

import com.paic.pay.merchant.entity.MerchantUser;
import com.paic.pay.merchant.exception.Error;
import com.paic.pay.merchant.exception.UserNotFoundException;
import com.paic.pay.merchant.mapper.UserRegisterMapper;
import com.paic.pay.merchant.vo.Pizza;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * Created tangxin pc on 2017/1/17.
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1")
public class RegisterController {

    @Autowired
    private UserRegisterMapper userRegisterMapper;

    /**
     * 商户注册
     * @return
     */
    @PostMapping(value = "/reg")
    public String reg(){
        return "reg";
    }

    @PostMapping(value = "/cache")
    public void cache(HttpServletRequest request){
        long date = System.currentTimeMillis();
        String threadIndex = request.getParameter("threadIndex");
        String url = request.getRequestURI();
        log.info("url:{} date:{} threadIndex:{}",url,date,threadIndex);
    }

    @GetMapping(value = "/uuid")
    public String uuid(){
        return UUID.randomUUID().toString();
    }

    @GetMapping(value = "/pizza")
    public Pizza getPizza(){
        Pizza pizza = new Pizza("中国比萨");
        return pizza;
    }

    @GetMapping(value = "/getUser")
    public MerchantUser getMerchantUser(String userId){
        MerchantUser merchantUser = userRegisterMapper.getMerchantUser(userId);
        return merchantUser;
    }

    @GetMapping(value = "/student")
    public String xml(String time){
        log.info("params:{}",time);
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><student>唐欣</student>";
    }


    @GetMapping(value = "/getUser2")
    public ResponseEntity<MerchantUser> getMerchantUser2(String userId){
        MerchantUser merchantUser = userRegisterMapper.getMerchantUser(userId);
        HttpStatus status = merchantUser!=null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
        return new ResponseEntity<>(merchantUser,status);
    }


    @GetMapping(value = "/getUser3")
    public ResponseEntity<?> getMerchantUser3(String userId){
        MerchantUser merchantUser = userRegisterMapper.getMerchantUser(userId);
        if(merchantUser==null){
            Error error = new Error(4,"用户["+userId+"]不存在");
            return new ResponseEntity<>(error,HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(merchantUser,HttpStatus.OK);
    }


    /**
     * 当查询结果为null时抛出异常 由异常处理器返回代码
     * @param userId
     * @return     正常返回:{"userId":1000,"userName":张三}
     *             异常返回:{"code":4,"message":"用户[16]不存在"}
     */
    @GetMapping(value = "/getUser4")
    public ResponseEntity<MerchantUser> getMerchantUser4(String userId){
        MerchantUser merchantUser = userRegisterMapper.getMerchantUser(userId);
        if(merchantUser==null){throw new UserNotFoundException(userId);}
        return new ResponseEntity<>(merchantUser,HttpStatus.OK);
    }

    /**
     * 异常处理器
     * @param e
     * @return
     */
    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<Error> userNotFound(UserNotFoundException e){
        String userId = e.getUserId();
        Error error = new Error(4,"用户["+userId+"]不存在");
        return new ResponseEntity<>(error,HttpStatus.NOT_FOUND);
    }
}

 

posted @ 2017-01-24 17:46  佛法无边  阅读(251)  评论(0编辑  收藏  举报