Spring4.0实战 rest相关
Posted 佛法无边
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); } }
以上是关于Spring4.0实战 rest相关的主要内容,如果未能解决你的问题,请参考以下文章
Spring 4 vs Jersey 用于 REST Web 服务
Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段