[英]How to redirect to external url if method return ResponseEntity<Object>
I have method in controller that return ResponseEntity Object 我在控制器中有返回ResponseEntity对象的方法
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Object> processInsert(HttpServletRequest request) {
return insertService.handleInsert(request);
}
public ResponseEntity<Object> handleInsert(HttpServletRequest request) {
Map<String, String[]> params = request.getParameterMap();
User user = createUser(params, request);
return new ResponseEntity<Object>(user, HttpStatus.OK);
}
Now when this execute it return me JSON in browser, how can i make it redirect me to some URL, for example www.google.com if i need to keep this method returning ResponseEntity Object 现在,当执行此命令时,它会在浏览器中返回JSON,如果我需要保持此方法返回ResponseEntity对象,如何使其重定向到某些URL,例如www.google.com。
I don't think I understood your question completely, if you want a ResponseEntity to be returned and then redirect to another page, this info will be lost after redirecting. 我认为我不完全理解您的问题,如果您希望返回一个ResponseEntity然后重定向到另一个页面,则此信息将在重定向后丢失。 If you want your endpoint to be redirected to another page, you need to return a String type from your controller method, like this: 如果要将端点重定向到另一个页面,则需要从控制器方法返回String类型,如下所示:
@RequestMapping(path = { "/testRedirect" }, method = { RequestMethod.GET})
public String testRedirect() {
return "redirect:http://www.google.com";
}
If you want to display an error or something in case your program misbehaves due to incorrect input data or anything, you can just do the following: 如果您想显示错误或某些情况,以防由于输入数据不正确或其他原因导致程序异常,则可以执行以下操作:
@RequestMapping(path = { "/testRedirect" }, method = { RequestMethod.GET})
public String testRedirect(HttpServletResponse servletResponse) {
// if error occurs
servletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"explained error");
return null; // return null to indicate your program that the request has finished with the error.
// if all works correctly return the redirect String.
return "redirect:http://www.google.com";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.