• 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);
        }
    }
  • 相关阅读:
    《Microsoft Sql server 2008 Internals》读书笔记第六章Indexes:Internals and Management(4)
    《Microsoft Sql server 2008 Internals》读书笔记第六章Indexes:Internals and Management(9)
    《Microsoft Sql server 2008 Internals》读书笔记第六章Indexes:Internals and Management(3)
    《Microsoft Sql server 2008 Internals》读书笔记第六章Indexes:Internals and Management(5)
    《Microsoft Sql server 2008 Internals》读书笔记第六章Indexes:Internals and Management(7)
    vs2010正式版安装图解
    Winform部署mshtml程序集出错的一个解决方案
    InstallShield 2010集成.net Framework 4的安装包制作
    vs2010无法访问svn存储库的一次意外
    InstallShield集成.net Framework的安装包制作
  • 原文地址:https://www.cnblogs.com/fofawubian/p/6347579.html
Copyright © 2020-2023  润新知