简体   繁体   English

如果方法返回ResponseEntity,如何重定向到外部url <Object>

[英]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.

相关问题 如果controller方法返回ResponseEntity,如何使用spring重定向 - How to use spring redirect if controller method returns ResponseEntity Spring MVC - 如何在 ResponseEntity 方法中返回视图? - Spring MVC - How do I return a view in a ResponseEntity method? 如何在 ResponseEntity 中不返回空值? - How to not return null value in ResponseEntity? 如何在ResponseEntity中返回布尔值? - How to return boolean value in ResponseEntity? ResponseEntity 不为 Post 方法返回 json 响应 - ResponseEntity do not return json response for Post Method @ResponseBody,ResponseEntity Spring将Object作为JSON返回 - @ResponseBody , ResponseEntity Spring return Object as JSON 如何从具有 ResponseEntity&lt;&gt; 返回类型的控制器方法在同一类中调用`GetParam`方法 - How to call `GetParam` method from a controller method with ResponseEntity<> return type within the same class Feign抛出错误而不是返回ResponseEntity,如何回到调用者方法 - Feign throw error instead of return ResponseEntity, how to go back to the caller method Spring MVC:RequestBody + ResponseEntity:如何在没有某些obj字段的情况下返回对象/实体 - Spring MVC: RequestBody + ResponseEntity: How to return an object/entity without some of the obj fields 如何将 MessageResponse 类型转换为 ResponseEntity<!--?--> 返回类型 - how to convert MessageResponse type to ResponseEntity<?> return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM