Java Spring Boot Restcontroller RequestMapping执行了两次 [英] Java Spring Boot Restcontroller RequestMapping executed twice

查看:281
本文介绍了Java Spring Boot Restcontroller RequestMapping执行了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有RestController的Spring Boot应用程序和一个下载并传递图像的方法:

I have a Spring Boot Application with a RestController and a method which will download and pass an image:

@RestController
public class PictureController {

    @RequestMapping("/picture/{id}")
    public HttpEntity<byte[]> getImage(@PathVariable String id)  {

        logger.info("Requested picture : >> " + id + " <<");

        // !! Execute code for downloading  !! 

        // Create Headers...

        // return HttpEntity<byte[]>
    }
}

在日志文件中,我可以读到该方法已被执行两次,我不知道为什么。

In the logfiles I can read that the method is executed twice and I don't know why.

如果删除下载代码,只需执行一次正如预期的那样。

If I remove the code for downloading it gets executed just once as expected.

这是因为下载它需要一秒钟吗?

Is it because it takes a second to download it?

下载代码是。 ..

        byte[] response;

        try {
            URL url = new URL(....);

            InputStream in = new BufferedInputStream(url.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1 != (n = in.read(buf))) {
                out.write(buf, 0, n);
            }
            out.close();
            in.close();
            response = out.toByteArray(); 

我也尝试了几种解决方案,如...

I also tried several solutions like...

@RequestMapping(value = "/picture2/{id}", headers = "Accept=image/jpeg, image/jpg, image/png, image/gif") 
public @ResponseBody byte[] getArticleImage2(@PathVariable String id) {

我觉得可能有问题与HttpEntity但它是相同的行为。按预期工作,无需下载代码,但下载图像后会执行两次

I thought maybe a problem with HttpEntity but it's the same behaviour. Works as expected without code for downloading but with downloading an image it gets executed twice.

这是我的应用程序严重的性能问题.. 。:(

This is a serious performance issue of my application... :(

这里有什么问题?

推荐答案

问题取决于用于测试RestController的浏览器。
我使用的是Firefox ...并且Firefox倾向于在图像周围获得一些html。但该方法不会返回html,因此Firefox正在启动另一个请求...也用于寻找一个图标。

The problem depends on the Browser which is used to test the RestController. I'am using Firefox... and Firefox tend to get some html around an image. But the method doesn't return html and so Firefox is starting another request... also for looking for a favicon.

Internet Explorer例如并不关心它,并且该方法只按预期执行一次!

Internet Explorer e.g. doesn't care about it and the method is only executed once as expected!

所以我的问题不是真正的问题,因为稍后我的RestController提供的图像将被嵌入到一个有html和favicon的网站中。

So my problem is not a real problem because later my image delivered by the RestController will be embedded in a website which has html and a favicon.

这篇关于Java Spring Boot Restcontroller RequestMapping执行了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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