其他分享
首页 > 其他分享> > 注册——控制层

注册——控制层

作者:互联网

5.控制层

5.1json数据返回状态
package com.cy.store.utils;

import java.io.Serializable;

public class JsonResult<E> implements Serializable {

   private Integer state;

   private String message;

   private E data;

   public JsonResult(Integer state, E data) {
       this.state = state;
       this.data = data;
  }

   public JsonResult(Throwable e) {
       this.message = e.getMessage();
  }

   public JsonResult(Integer state) {
       this.state = state;
  }

   public JsonResult() {
  }

   public Integer getState() {
       return state;
  }

   public void setState(Integer state) {
       this.state = state;
  }

   public String getMessage() {
       return message;
  }

   public void setMessage(String message) {
       this.message = message;
  }

   public E getData() {
       return data;
  }

   public void setData(E data) {
       this.data = data;
  }
}
5.2UserController
package com.cy.store.controller;


import com.cy.store.entity.User;
import com.cy.store.service.IUserService;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UsernameDeplicatedException;
import com.cy.store.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@Controller
@RestController
@RequestMapping("users")
public class UserController extends BaseController{


   @Autowired
   private IUserService iUserService;

   @RequestMapping("reg")
   public JsonResult<Void> reg(User user)
  {
       iUserService.reg(user);
       return new JsonResult<>(OK);
  }

   /*@RequestMapping("reg")
   public JsonResult<Void> reg(User user)
   {
       JsonResult<Void> result = new JsonResult<>();

       try {
           iUserService.reg(user);
           result.setState(200);
           result.setMessage("注册成功");
       } catch (UsernameDeplicatedException e) {
           result.setState(4000);
           result.setMessage("用户名占用");
       }
       catch (InsertException e)
       {
           result.setState(5000);
           result.setMessage("未知异常");
       }
       return result;
   }*/

}
5.3BaseController
package com.cy.store.controller;

import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.ServiceExcepiton;
import com.cy.store.service.ex.UsernameDeplicatedException;
import com.cy.store.utils.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController {
   public static final int OK = 200;

   @ExceptionHandler(ServiceExcepiton.class) //用于统一处理抛出的异常
   public JsonResult<Void> handle(Throwable e)
  {
       JsonResult<Void> result = new JsonResult<>();

       if(e instanceof UsernameDeplicatedException)
      {
           result.setState(4000);
           result.setMessage("用户名已经占用");
      }
       else if (e instanceof InsertException)
      {
           result.setState(5000);
           result.setMessage("注册时候产生未知异常");
      }
       return result;
  }

}

 

标签:控制,JsonResult,cy,result,注册,import,com,public
来源: https://www.cnblogs.com/shuangshuangzi/p/15977931.html