【发布时间】:2020-02-29 10:00:39
【问题描述】:
我有 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)
【问题讨论】:
-
如果你想让当前代码工作,你需要做两件事,1.) 将方法返回类型更改为字符串 2.) return employee.toString(()
-
第一件事是它是一个
RestController,因此它作为简单的表示。如果您想以 html 形式提供内容,请更改为@Controller
标签: java spring spring-boot spring-mvc spring-rest