ResponseStatusExceptionResolver

@ResponseBody
	@RequestMapping(value="/getUserInfoById.do")
	public User getUserById(Long userId)
			throws com.controller.interceptor.LoginException{
		User user = new User();
		if(userId == null){
			throw new LoginException();
		}
		user = userService.selectByPrimaryKey(userId);
		if(user == null){
			throw new LoginException();
		}
		return user;
	}
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;


@ResponseStatus(value = HttpStatus.UNAUTHORIZED reason="没有权限")
public class LoginException extends Exception {


}
抛出的异常有@ResponseStatus注解,则由ResponseStatusExceptionResolver解析到。最后响应HttpStatus.UNAUTHORIZED代码给客户端。HttpStatus.UNAUTHORIZED代表响应码401,无权限。另外还有其他几种状态,对应不同状态码
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页