如何在控制器上获取对端点的请求,该请求被定义为web.xml上的默认servlet映射? [英] How to get a request on controller for endpoint which is defined as default servlet-mapping on web.xml?

查看:44
本文介绍了如何在控制器上获取对端点的请求,该请求被定义为web.xml上的默认servlet映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用第三方API提供程序提供特定服务的服务器上工作.第三方发送图像端点.例如."/media/image/image.jpg" .该图片可在第三方基本URL上获得.

I am working on a server where we are using a third party API provider for a particular service. The third party send a image endpoint. Eg. "/media/image/image.jpg". The image is available on the third party base URL.

我需要在我们的基本URL上提供该图像.为此,我有一个控制器.

I need to make the image available on our base url. For that I have a controller.

@RequestMapping(value = "/media/movie/{imageName}", method = RequestMethod.GET)
public void getMovieImage(@PathVariable("imageName") String imageName, HttpServletResponse response) throws IOException {
    String type = imageName.split(".")[imageName.split("\\.").length - 1];
    String imageUrl = getBaseUrl() + imageName;
    BufferedImage image = ImageIO.read(new URL(imageUrl));
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("image/" + type);
    ImageIO.write(image, type, response.getOutputStream());
}

但是问题是 *.jpg 被定义为web.xml上的默认servlet映射.

But the problem is *.jpg is defined as a default servlet mapping on web.xml.

<servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>*.jpg</url-pattern>
</servlet-mapping>`

因此请求不会到达控制器并显示404.

So the request don't get to the controller and shows 404.

如何在控制器上获取请求,或者是否有其他替代方法解决问题?

How can I get the request on my controller or is there any alternative way to the problem?

推荐答案

您可以添加过滤器的初始化参数 excludedUrls 并检查过滤器是否不在排除列表中.请参见完整示例

You can add filter's init parameter excludedUrls and check in filter if it isn't in exclsion list. See full example

if(!excludedUrls.contains(path))

这篇关于如何在控制器上获取对端点的请求,该请求被定义为web.xml上的默认servlet映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆