ResponseEntity, how to obtain the body in html

12,101

Solution 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);
    }
}

Solution 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.

Share:
12,101

Related videos on Youtube

Santiago Gil
Author by

Santiago Gil

I am currently a fourth year undergraduate studying Computer Engineering at the University of Zaragoza. My score is 9.48, and I have been awarded with a total of 22 distinctions. My expectations are to get a Master's degree and then to doctorate.

Updated on September 15, 2022

Comments

  • Santiago Gil
    Santiago Gil over 1 year

    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?

  • Santiago Gil
    Santiago Gil over 8 years
    Something like this is what I wanted, but doesn't work. 1) I get incompatible type error in MediaType.TEXT_HTML (it expects MediaType but it is passed String, altought in the API that should work...). 2) I comment the content type line, but the desired data is not shown in browser as you can see. You can check my public repo here
  • Ralph
    Ralph over 8 years
    1) you need to use: org.springframework.http.MediaType
  • Ralph
    Ralph over 8 years
    2) do you have an 404 error page definied in your web,xml?
  • Santiago Gil
    Santiago Gil over 8 years
    1) Solved, thanks 2) No, I redirect the user with response.sendRedirect("notReachable.html");. I just want to add the phrase "Url is not reachable from <desired date>" in the image I have recently sended. Still doesn't show nothing. 3) I have same html in another pages, and they work. I think there is a confusion: I have both tags head and body, and inside body I have tag header.
  • Santiago Gil
    Santiago Gil over 8 years
    Okey, if i don't redirect him, it is shown in browser you are right. But how can I get it shown in the page desired?
  • GuiRitter
    GuiRitter over 4 years
    Thanks, it worked for me! But I only works in a @RestController, and I wanted it to work in a @RepositoryRestController.
  • Ralph
    Ralph over 4 years
    @GuiRitter - please raise a new question, something like "How to add an ExceptionHandler for Spring Data Rest Controllers?"
  • GuiRitter
    GuiRitter over 4 years
    @Ralph Thanks but it's not necessary. I'm not interested in ExceptionHandlers. I just wanted to return a simple HTML with a redirect to another URL. I managed to do that with this