1

I have Spring Rest controller,as below :

@RestController
@RequestMapping(value = "/v1/files")
public class DataReader {

    @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
    public Employee readData () {
        Employee employee = new Employee();
        employee.setName("GG");
        employee.setAddress("address");
        employee.setPostCode("postal code");
        return employee;
    }
}

Basically,I want this controller to return html content.However,when I hit the URI from the browser or from postman,I get following exception :

There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:316)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:181)
javaguy
  • 927
  • 2
  • 16
  • 37
  • If you want current code to work, you need to two things, 1.) change the method return type to string 2.) return employee.toString(() – Shailesh Chandra Nov 04 '19 at 05:34
  • Does this answer your question? [How to return a html page from a restful controller in spring boot?](https://stackoverflow.com/questions/38700790/how-to-return-a-html-page-from-a-restful-controller-in-spring-boot) – Vinay Prajapati Nov 04 '19 at 05:58
  • First thing is that it's a `RestController` and hence it serves things as plain representation. If you want to serve content as html then change to `@Controller` – Vinay Prajapati Nov 04 '19 at 06:21

2 Answers2

0

In order to serve an html content , if the content is static then you could use the controller endpoint like :

@GetMapping(value = "/")
public Employee readData () {
    return "employee";
}

and springboot will return the static html page named "employee". But in your case you need to return a modelandview map to have the dynamic data with the html rendered like :

@GetMapping(value = "/")
public Employee readData (Model model) {
    Employee employee = new Employee();
    employee.setName("GG");
    employee.setAddress("address");
    employee.setPostCode("postal code");
    model.addAttribute("employee",employee)
    return "employee";
}

Also remove the @RestController annotation from your class and add @Controller.

Otherwise, if your usecase requires that you return an html content from a REST endpoint then use like :

@RestController
@RequestMapping(value = "/v1/files")
public class DataReader {

    @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
    public Employee readData () {
       // employees fetched from the data base
          String html = "<HTML></head> Employee data converted to html string";
          return html;
    }
}

or use return ResponseEntity.ok('<HTML><body>The employee data included as html.</body></HTML>')

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
  • yes,my use case is to return Html from Rest endpoint.so you mean we have to manually convert Object data into Html and then return html as String. – javaguy Nov 04 '19 at 05:38
  • @user2603985 yes , that is one way of doing this.When you provide the produces parameter as TEXT_HTML_VALUE it expects a String equivalent of TEXT_HTML. – Ananthapadmanabhan Nov 04 '19 at 06:41
0

Your return type of method is object Employee. If you need to return HTML content choose any of below options

  1. Convert your controller from @RestController to @Controller, add spring MVC dependency, configure template engine, create your htmls and return it from controller

  2. Instead of returning Employee object from REST controller, use Streams to send HTML as byte array in Response entity.

Defaulter
  • 358
  • 1
  • 4
  • 17