ResponseEntity, how to obtain the body in html

蓝咒 提交于 2020-02-04 02:52:50

问题


I want to show in browser the body of a ResponseEntity returned by a Controller (using Spring):

return new ResponseEntity<>(l.getReachableDate(), HttpStatus.NOT_FOUND);

l.getReachableDate() returns a Date type, and I want to show it in a way like:

<header>
    <h1><span>Url is not reachable from</span> <!-- body --> </h1>
</header>

How can I get it shown?


回答1:


I still not understand why you want to do this, but this way it should work

@RequestMapping(value="/controller", method=GET)
public ResponseEntity<String> foo() {
    String content = 
           "<header>"
         + "<h1><span>Url is not reachable from</span>" +  l.getReachableDate() + "</h1>"
         + "</header>";
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.TEXT_HTML);

    return new ResponseEntity<String>(content, responseHeaders, HttpStatus.NOT_FOUND);
}

after some comments....

Instead of redirecting the user to an resource not found page, it would been better to have an ResourceNotFoundRuntimeException (extends RuntimeException) and register an MVC exception handler (that is what prem kumar suggested, but wihtout the customized exception html text):

public class ResourceNotFoundRuntimeException extends RuntimeException{
...
}

The handler:

@ControllerAdvice
public class ExceptionHandlerController {

    @ExceptionHandler(ResourceNotFoundRuntimeException .class)
    public ResponseEntity<String> resourceNotFoundRuntimeExceptionHandling(){
        String content = 
               "<header>"
             + "<h1><span>Url is not reachable from</span>" +  l.getReachableDate() + "</h1>"
             + "</header>";
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.TEXT_HTML);

        return new ResponseEntity<String>(content, responseHeaders, HttpStatus.NOT_FOUND);
    }
}



回答2:


If you want the entire body from server side using spring, then you can throw a custom ResourceNotFoundException and handle it using a spring exception handler.

Check the following links:

https://stackoverflow.com/a/21115267/5039001

The following link gives you different ways to do it.

https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

If you want different bodies for different urls, then you can have a property html body in ResourceNotFoundException and pass the body as a constructor argument and throw the exception. In exception handler, you can retrieve this body and build the http response message.

public class ResourceNotFoundException extends RuntimeException{
    private String htmlBody;
    public ResourceNotFoundException(String htmlBody){
        //super(...)
        this.htmlBody = htmlBody;
    }
}

You can now reuse this exception globally. Note that you need to setup a corresponding exception handler for this exception for which you can visit the above shared links.



来源:https://stackoverflow.com/questions/34691579/responseentity-how-to-obtain-the-body-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!