【发布时间】:2017-12-07 08:27:54
【问题描述】:
做API Rest 测试,有时我会遇到一个情况,当一个Rest 函数失败,但是连第一个断点都没有到达。显然,一些初步设置失败了。但究竟是什么?
这里是函数的头部:
@RequestMapping(value = { "/export" }, method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
public Object exportConfiguration(@RequestBody ConfigurationExport configurationExport) {
try {
// at return here stands the breakpoint that is never reached:
return configurationExporterProvider.export(configurationExport);
请求的正文是:
{}
作为回应,我得到:
<td>HTTP ERROR: 415 Unsupported Media Type
<p>Problem accessing /cc/api/configuration/export
<pre id="message">Unsupported Media Type</pre>
好的,我想我 - 尝试将正文加载到 configurationExport 时发生了一些事情。但是该类构造函数中没有抛出错误:
private ConfigurationExport() {
documentTypes = new ArrayList<>();
valueSets = new ArrayList<>();
configuration = new Configuration();
}
还有由 Lombok 创建的 setter 和 getter。当然,Lombok 对任何媒体类型一无所知。
我同意应用程序向我返回带有错误消息的响应。但它出现在哪里?我应该在哪里以及如何寻找它?
【问题讨论】:
-
我假设命名法,您使用的是 Spring。所以我认为,在这种情况下,您忘记了
RequestMapping中的消耗(例如consumes = "application/json")注释,因为您正在传递给 REST尚不支持方法exportConfiguration数据。 -
我可以从请求端以某种方式设置 consumer = "application/json" 吗?对不起,可能是愚蠢的问题。
-
类似这样的东西:
@RequestMapping(value = { "/export" }, method = RequestMethod.POST, produces = "text/plain;charset=UTF-8", consumes = "application/json")。但这只是一个例子,我不知道你是否消费json ;-) -
是的,它应该消耗 json。但刚才我检查了 - 还有其他函数在函数的 Spring 标头中没有“消耗”,看起来也一样,但不要返回该错误并到达函数体中的断点。
-
我认为不仅是消耗spring rest方法的,也是接受lombok查询的,它与http方法中的accept header有关
标签: java spring rest api debugging