[英]Manage REST response with Spring MVC
我正在使用Spring創建REST api。 目前,我使用此結構來提供文件瀏覽器服務:
FileModel.java
package hello;
public class FileModel {
private String name;
private Long lastUpdate;
private Long size;
/**
* Void Constructor
*/
public FileModel() {
}
/**
* Parametrized constructor
* @param name
* @param created
* @param lastUpdate
* @param size
*/
public FileModel(String name, Long lastUpdate, Long size) {
super();
this.name = name;
this.lastUpdate = lastUpdate;
this.size = size;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the lastUpdate:A long value representing the time the file was last modified,
* measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
*/
public Long getLastUpdate() {
return lastUpdate;
}
/**
* @param lastUpdate the lastUpdate to set
*/
public void setLastUpdate(Long lastUpdate) {
this.lastUpdate = lastUpdate;
}
/**
* @return the size in bytes
*/
public Long getSize() {
return size;
}
/**
* @param size the size to set
*/
public void setSize(Long size) {
this.size = size;
}
}
FileServices.java
package hello;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.ArrayList;
import org.springframework.stereotype.Service;
@Service
public class FileServices {
public ArrayList<FileModel> getAllFiles(String path) throws FileNotFoundException {
ArrayList<FileModel> files=new ArrayList<FileModel>();
File directory = new File(path);
if (directory.exists()){
//get all the files from a directory
File[] fList = directory.listFiles();
//check if list is null
for (File file : fList){
if (file.isFile()){
FileModel f=new FileModel(file.getName(),file.lastModified(),file.length());
files.add(f);
}
}
return files;
}
else throw new ResourceNotFoundException(path);
}
}
ResourceNotFoundException.java
package hello;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ResourceNotFoundException(String path){
super("The specified path: "+ path +" doesn't exist");
}
}
文件管理器
package hello;
import java.io.FileNotFoundException;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FileManager {
@Autowired
private FileServices file;
@RequestMapping(value = "/files", method = RequestMethod.GET)
public Collection<FileModel> getAllFiles(@RequestParam(value="path", defaultValue="/home") String path) throws FileNotFoundException {
return file.getAllFiles(path);
}
}
響應是這樣的錯誤消息
> { "timestamp": 1442560477794, "status": 404, "error": "Not
> Found", "exception": "hello.ResourceNotFoundException", "message":
> "The specified path: /home doesn't exist", "path":
> "/MatlabLib/files" }
或這個。
[
{
"name": "apache-tomcat-8.0.26-windows-x64.zip",
"lastUpdate": 1441282759343,
"size": 10470002
},
{
"name": "desktop.ini",
"lastUpdate": 1441357976196,
"size": 282
}
]
鑒於我必須通過其他Java或Matlab代碼管理該Web服務,因此我需要一個自定義響應,例如狀態,錯誤,異常,消息,正文,以便我可以檢查狀態代碼並了解是否有錯誤或更少。 在Spring中,有一種構建方法可以做到嗎?
謝謝
更新:我創建了兩個響應類,一個用於ErrorResponse,一個用於具有不同fied數量的Response。 然后我用
@ControllerAdvice
public class ErrorController {
/**
*
* @param e: exception thrown
* @return ErroreResponse
*/
@ExceptionHandler(Exception.class)
public @ResponseBody ErrorResponse errorHandler(Exception e){
//Make the exception by buildErrorResponse
return ErrorResponseBuilder.buildErrorResponse(e);
}
在ErrorResponseBuilder中做了以下方法:
/**
* Build exception response beginning from exception
* @param e exception thrown
* @return ErrorResponse: response of an exception
*/
public static ErrorResponse buildErrorResponse(Exception e){
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
return new ErrorResponse(HttpStatusManager.getHttpCode(e),e.getClass().getName(),e.getMessage(),errors.toString());
}
在HttpStatusManager中,我實現了這一點:
public HttpStatusManager() {
}
/**
* Add to this class all new exception associating a code error
* @param exception
* @return
*/
public static int getHttpCode(Exception exception){
if (exception instanceof ResourceNotFoundException);
return HttpStatus.NOT_FOUND.value();
}
所以從控件使用這條簡單的線
@RequestMapping(value = "/files", method = RequestMethod.GET)
public Response<Collection<FileModel>> getAllFiles(@RequestParam(value="path", defaultValue="/home") String path) throws ResourceNotFoundException {
Collection<FileModel> result;
result = file.getAllFiles(path);
return new Response<Collection<FileModel>>(HttpStatus.OK.value(),result);
}
你怎么看?
您可以使用具有良好API的javax.ws.rs.core.Response
。 但就我個人而言,我寧願創建一個自定義類來處理此類響應。
當然,您需要將Jackson JSON API
附加到項目中,以便可以構建返回對象的方法。 另外,您還必須在spring配置文件中配置messageConverters
。
UPDATE
public class Response {
private Object responseBody;
private String message;
private int responseCode;
public Response() {
responseCode = 200; //default HTTP 200 OK
}
public Object getResponseBody() {
return responseBody;
}
public void setResponseBody(Object responseBody) {
this.responseBody = responseBody;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getResponseCode() {
return responseCode;
}
public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}
}
使用例
@RequestMapping(value = "/files", method = RequestMethod.GET)
public Response getAllFiles(@RequestParam(value="path", defaultValue="/home") String path) {
Response response = new Response();
try {
Collection<FileModel> files = file.getAllFiles(path);
response.setResponseBody(files);
} catch (FileNotFoundException e) {
Utils.setErrMessage(response, e);
}
return response;
}
您的setErrMessage
函數可能看起來像這樣
public void setErrMessage(Response response, Exception e) {
if(e instanceof NullPointerException) {
response.setErrCode(400); //HTTP 400 Bad Request
}
else if(e instanceof FileNotFoundException || ...) {
response.setErrCode(500); //HTTP 500 Interval Server Error
}
...
response.setMessage(e.getMessage);
}
這只是一個總的想法,您可以隨意更改它。
我有一個通用的響應DTO,我使用Spring @ControllerAdvice
和@ExceptionHandler
在其余服務中實現了全局錯誤處理機制。
全局錯誤處理程序代碼:
@ControllerAdvice
public class WSControllerAdvice {
private static Logger logger = Logger.getLogger(SearchControllerAdvice.class);
@ExceptionHandler(Exception.class)
public @ResponseBody WSResponse<String> errorHandler(Exception e){
WSResponse<String> response = new WSResponse<String>();
logger.error("Exception occured "+e.getMessage());
response.setResponseStatus(WSResponseCode.UNHANDLED_EXCEPTION);
response.setCount(0);
response.getErrors().add("Unhandled Search Error : " + e.getMessage());
return response;
}
}
通用響應DTO
public class WSResponse<T> {
private String responseStatus;
private long count;
private long totalNumFound;
private T results ;
private List<String> errors = new ArrayList<>();
private String nextPageMark;
private List<Facet> facets = new ArrayList<>();
}
吸氣劑塞特刪除
無論結構是否發生異常,您都具有使用此結構的一致響應格式。
有兩種方法可以實現此目的:
捕獲異常並返回描述異常詳細信息的POJO,如下所示:
@RequestMapping(value = "/files", method = RequestMethod.GET) public Object getAllFiles(@RequestParam(value="path", defaultValue="/home") String path) throws FileNotFoundException { try{ }catch(ResourceNotFoundException rnfEx){ return new ExceptionDTO("ERR-001","Resource not found"); } }
其中ExceptionDTO
表示將轉換為JSON的POJO
使用@ExceptionHandler
處理相同的功能。 范例: http : //www.journaldev.com/2651/spring-mvc-exception-handling-exceptionhandler-controlleradvice-handlerexceptionresolver-json-response-example
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.