【问题标题】:Hybris + swagger integration swagger-ui.html UnknownResourceErrorHybris + swagger 集成 swagger-ui.html UnknownResourceError
【发布时间】:2018-06-08 23:45:36
【问题描述】:

我正在尝试将 swagger 集成到 MYcommercewebservices 中。 我阅读了帖子并完成了上面列出的所有步骤,但仍然出现此错误。

https://localhost:9002/mycommercewebservices/v2/v2/api-docs 工作正常。 https://localhost:9002/mycommercewebservices/v2/swagger-ui.html - 返回 UnknownResourceError。

此外 - 如果我导航到 https://localhost:9002/mycommercewebservices/swagger-ui.html(没有“v2”),它会向我显示这条消息(javascript 警报):

无法推断基本 URL。这在使用动态 servlet 时很常见 注册或当 API 位于 API 网关后面时。基本网址是 提供所有 swagger 资源的根。例如如果 API 可在http://example.org/api/v2/api-docs 获得,然后 基本 URL 是 http://example.org/api/。请输入位置 手动:

我找到了这个控制器,可能部分问题出在其中,因为当我导航到 https://localhost:9002/mycommercewebservices/v2/swagger-ui.html 时它抛出了异常

@Controller
public class DefaultController
{
    @RequestMapping
    public void defaultRequest(final HttpServletRequest request)
    {
        throw new UnknownResourceException("There is no resource for path " + YSanitizer.sanitize(request.getRequestURI()));
    }
}

现在我禁用了控制器,但仍然有同样的异常,但现在它是 json 格式而不是 .xml。

谢谢!

【问题讨论】:

  • 我不是。但是 v2/v2 是 api-docs 的正确路径 - 一个用于服务版本,另一个用于 api-docs 版本(至少 hybris 上的所有资源都有助于说明这一点)。
  • 所以 api-docs 可以在 localhost:9002/mycommercewebservices/v2/v2/api-docs 获得吗?
  • 是的,它可用。 Hybris 看到 api url 和文档,但 UI(swagger-ui.html 页面) 不起作用。

标签: swagger swagger-ui hybris


【解决方案1】:

主要问题在于 DefaultController(在 MYcommercewebservices 中)

@Controller
public class DefaultController
{
    @RequestMapping
    public void defaultRequest(final HttpServletRequest request)
    {
        throw new UnknownResourceException("There is no resource for path " + YSanitizer.sanitize(request.getRequestURI()));
    }
}

它正在捕获我的请求并抛出异常。
当我禁用此控制器时,我继续收到异常,但现在它是 json 格式(之前是 xml)。

比我添加这个到 springmvc-v2-servlet.xml

<mvc:default-servlet-handler/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

现在 UI 可以正常工作了!

在这之前还有另一个操作,但你可以在 hybris 专家中找到它们(相当大的帖子)。

【讨论】: