How to get a request on controller for endpoint which is defined as default servlet-mapping on web.xml?

浪尽此生 提交于 2019-12-13 03:41:19

问题


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.

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());
}

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>`

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?


回答1:


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))


来源:https://stackoverflow.com/questions/56768717/how-to-get-a-request-on-controller-for-endpoint-which-is-defined-as-default-serv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!