简体   繁体   中英

How to redirect to external url if method return ResponseEntity<Object>

I have method in controller that return ResponseEntity Object

@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

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. If you want your endpoint to be redirected to another page, you need to return a String type from your controller method, like this:

@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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM