【问题标题】:Rest function fails before the start休息功能在开始前失败
【发布时间】: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


【解决方案1】:

在调用您的方法之前,Spring 会对请求和请求标头进行多次验证。如果需要,您可以在 Spring 类方法 org.springframework.web.servlet.DispatcherServlet.doDispatch() 中设置断点以进行调试。

【讨论】:

  • 我可以打开一些我的检查,以便在那里放置断点吗?因为我在项目中没有 DispatcherServlet 文件。
  • DispatcherServlet 是一个 Spring 类,我假设您使用的是 Spring。如果您使用 Spring,您可以在 IDE 中打开该类并设置断点。如果您使用 JavaEE,则没有 DispatcherServlet,但是...一些其他 Servlet。
  • 另一种声明过滤器的方式,并在 Servlet 之前检查请求
  • 是的,是春天。我发现它在 Spring-webvmc jar 中,在 springframework.web.servlet 包中。我必须将断点放入的方法是什么?
  • 该变体有效。请在答案中添加有关 jar 和包的信息以及 doDispatch 作为启动方法。非常感谢。
猜你喜欢
  • 2020-05-21
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多