@ResponseStatus、HttpServletResponse和ResponseEntity 返回自定义信息

目的

在实现查询 ID 返回指定 ID 对象的信息时,如果这个 ID 只到 8,当我查询 9 的时候,应该是不存在的,这个时候返回的信息应该是提示不存在,而且状态码不能是 200 ,因为这也是一个异常,但是不能抛出异常出现 java 的异常界面。
效果图:

@ResponseStatus

首先 @ResponseStatus 是可以实现指定状态码,并且有异常会抛出系统自带的异常,当我查询的结果不存在想表示为 404 的时候代码如下:

	@RequestMapping(value = "/getbyid/{id}", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getbyid( HttpServletRequest request,@PathVariable(value="id")int idnum,HttpServletResponse response){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Movie movie=movieService.getMovieById(idnum);
		if(movie==null){
			//如果没查询到结果
			throw new MovieException("");
		}else{
			modelMap.put("movie",movie);
		}
		return modelMap;
	}

MovieException.java

package com.caeser.upmovie.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason="not found 10001")
public class MovieException  extends RuntimeException{
	private static final long serialVersionUID = -9141529090618182905L;
	public MovieException(String msg){
		super(msg);
	}
}

当我们访问 http://localhost:8080/upmovie/movie/getbyid/9 结果如下:

在这里插入图片描述
虽然可以看到提示信息,但是结果不够友好,因为当前后端分离之后,前端人员不是要帮你调试程序,而是想通过接口获取指定信息,如果这个信息不存在,那么只需要知道不存在,而且告诉对方这是一个异常就行了。怎样可以实现指定状态码,还能返回自定义异常呢?

HttpServletResponse

HttpServletResponse 是 javax.servlet 下的一个接口
使用它就可以指定返回的状态码,还可以返回自定义的信息,使用方法如下:

@RequestMapping(value = "/getbyid/{id}", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getbyid( HttpServletRequest request,@PathVariable(value="id")int idnum,HttpServletResponse response){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Movie movie=movieService.getMovieById(idnum);
		if(movie==null){
			//如果没查询到结果
			response.setStatus(404);
			//UrlReturnMsg是我定义的静态类只保存了两个字符串
			//MovieStateEnum只保存了自定义的状态码和提示信息
			modelMap.put(UrlReturnMsg.stateCode, MovieStateEnum.NOTEXIT.getState());
			modelMap.put(UrlReturnMsg.errorMsg, MovieStateEnum.NOTEXIT.getStateInfo());	
		}else{
			modelMap.put("movie",movie);
		}
		//由于使用了 ResposeBody 返回值以 JSON 串的形式展示
		return modelMap;
	}

当我们访问 http://localhost:8080/upmovie/movie/getbyid/9
结果如下:

在这里插入图片描述在这里插入图片描述

还有一种方式,ResponseEntity<Map<String,Object>> 不建议使用

也可以实现自定义返回信息并指定状态码,直接上代码

@RequestMapping(value = "/getbyidentity/{id}", method = RequestMethod.GET)
	private ResponseEntity<Map<String,Object>> getbyidentity( @PathVariable(value="id")int idnum){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Movie movie=movieService.getMovieById(idnum);
		if(movie==null){
			//如果没查询到结果
			modelMap.put(UrlReturnMsg.stateCode, MovieStateEnum.NOTEXIT.getState());
			modelMap.put(UrlReturnMsg.errorMsg, MovieStateEnum.NOTEXIT.getStateInfo());	
			return new ResponseEntity<Map<String,Object>>(modelMap,HttpStatus.NOT_FOUND);
		}else{
			modelMap.put("movie",movie);
		}
		return new ResponseEntity<Map<String,Object>>(modelMap,HttpStatus.OK);
	}

返回的结果也是一样的,状态码404,返回自定义信息,这种方式也是可以的。我个人比较喜欢使用 @ResponseBody + HttpServletResponse 的方式,原因很简单,功能分开使得代码更清晰。

猜你喜欢

转载自blog.csdn.net/Caeser110/article/details/101393276
今日推荐