MediaType HTML的HttpMediaTypeNotAcceptableException

我有Spring Rest控制器,如下所示:

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

基本上,我希望此控制器返回html内容。但是,当我从浏览器或邮递员访问URI时,出现以下异常:

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)
wenhong1314 回答:MediaType HTML的HttpMediaTypeNotAcceptableException

为了提供html内容,如果内容是静态的,则可以使用控制器端点,例如:

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

和springboot将返回名为“ employee”的静态html页面。但是在您的情况下,您需要返回一个modelandview映射,以使用html呈现动态数据,如:

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

还要从您的班级中删除@RestController批注,然后添加@Controller

否则,如果您的用例要求您从REST端点返回html内容,则使用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;
    }
}

或使用return ResponseEntity.ok('<HTML><body>The employee data included as html.</body></HTML>')

,

您的方法的返回类型为对象Employee。如果您需要返回HTML内容,请选择以下任一选项

  1. 将您的控制器从<?xml version="1.0" encoding="utf-8"?> <Users xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ThisUsers> <User> <ID>1</ID> <FirstName>User 1</FirstName> <LastName>Last Name 1</LastName> <DOB>1990-11-04T08:16:09.1099698+03:00</DOB> </User> <User> <ID>2</ID> <FirstName>User 2</FirstName> <LastName>Last Name 2</LastName> <DOB>1991-11-04T08:16:09.1109688+03:00</DOB> </User> <User> <ID>3</ID> <FirstName>User 3</FirstName> <LastName>Last Name 3</LastName> <DOB>1992-11-04T08:16:09.1109688+03:00</DOB> </User> <User> <ID>4</ID> <FirstName>User 4</FirstName> <LastName>Last Name 4</LastName> <DOB>1993-11-04T08:16:09.1109688+03:00</DOB> </User> </ThisUsers> </Users> 转换为@RestController,添加spring MVC依赖项,配置模板引擎,创建html并从控制器返回它

  2. 不是使用REST控制器返回Employee对象,而是使用Streams在响应实体中将HTML作为字节数组发送。

本文链接:https://www.f2er.com/3169003.html

大家都在问