0

我正在尝试返回此处显示的图像https://www.baeldung.com/spring-mvc-image-media-data

那里显示的所有方法都可以正常工作,除了最后一种。

    @ResponseBody
    @RequestMapping(value = "/image-resource", method = RequestMethod.GET)
    public Resource getImageAsResource() {
       return new ServletContextResource(servletContext, "/WEB-INF/images/image-example.jpg");
    }

或者,如果我们想要更多地控制响应头:

    @RequestMapping(value = "/image-resource", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Resource> getImageAsResource() {
        HttpHeaders headers = new HttpHeaders();
        Resource resource = 
          new ServletContextResource(servletContext, "/WEB-INF/images/image-example.jpg");
        return new ResponseEntity<>(resource, headers, HttpStatus.OK);
    }

使用此方法时,我收到错误消息:根据请求中收到的主动协商标头字段,目标资源没有用户代理可以接受的当前表示,并且服务器不愿意提供默认表示.

我尝试了很多选择,但没有一个有帮助。我研究了这些页面

HTTP 状态 406 - 在 Spring MVC 中不可接受

使用 spring-boot-starter-web “找不到可接受的表示”

找不到可接受的代表

和许多其他人,这些页面上的答案都没有帮助解决问题。

如果我做:

    @GetMapping(value = "/image4", produces = "image/jpeg")

然后我得到错误:No converter for [class org.springframework.web.context.support.ServletContextResource] with preset Content-Type 'null'

如果我做:

    @GetMapping(value = "/image4", consumes = "image/jpeg")

然后我收到错误消息:源服务器拒绝为请求提供服务,因为有效负载的格式不受目标资源上此方法的支持。

这是我的 github 项目https://github.com/MyTestPerson/images

请告诉我我做错了什么?

图像类

    @Controller
    public class Image {


        @Autowired
        ServletContext servletContext;



        @ResponseBody()
        @GetMapping(value = "/image4")
        public Resource getImage4() {

            return new ServletContextResource(servletContext, "/WEB-INF/image/jackson.jpg");

        }


        @ResponseBody
        @GetMapping(value = "/image5")
        public ResponseEntity<Resource> getImage5() {

            HttpHeaders headers = new HttpHeaders();
            Resource resource = new ServletContextResource(servletContext, "/WEB-INF/image/jackson.jpg");

            return new ResponseEntity<>(resource, headers, HttpStatus.OK);

        }


    }

RootConfig.class

    @EnableWebMvc
    @Configuration
    public class RootConfig implements WebMvcConfigurer {


        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(byteArrayHttpMessageConverter());
        }

        @Bean
        public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
            ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
            arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
            return arrayHttpMessageConverter;
        }

        private List<MediaType> getSupportedMediaTypes() {
            List<MediaType> list = new ArrayList<MediaType>();
            list.add(MediaType.IMAGE_JPEG);
            list.add(MediaType.IMAGE_PNG);
            list.add(MediaType.APPLICATION_OCTET_STREAM);
            return list;
        }

    }

POM.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.images</groupId>
        <artifactId>images</artifactId>
        <version>1.0-SNAPSHOT</version>

        <build>
            <plugins>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${encoding}</encoding>
                    </configuration>
                </plugin>

            </plugins>
        </build>

        <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <java.version>11</java.version>
            <encoding>UTF-8</encoding>
        </properties>


        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-framework-bom</artifactId>
                    <version>5.3.9</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-bom</artifactId>
                    <version>5.5.1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.junit</groupId>
                    <artifactId>junit-bom</artifactId>
                    <version>5.7.2</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

        <dependencies>

            <!--        Spring Framework-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
            </dependency>



            <!--        Freemarker-->
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.31</version>
            </dependency>



            <!--        Servlet-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
            </dependency>


            <!-- Apache Commons IO-->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.11.0</version>
            </dependency>


            <!-- com fasterxml jackson data format-->
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
                <version>2.12.4</version>
            </dependency>



            <!--        Testing-->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
                <version>3.19.0</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>3.8.0</version>
                <scope>test</scope>
            </dependency>


            <!--        Logging-->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.30</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.30</version>
            </dependency>
        </dependencies>

    </project>
4

1 回答 1

1

感谢@Andreas

RootConfig.class

    @EnableWebMvc
    @Configuration
    public class RootConfig implements WebMvcConfigurer {


        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(byteArrayHttpMessageConverter());
            converters.add(resourceHttpMessageConverter());
        }

        @Bean
        public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
            ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
            arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
            return arrayHttpMessageConverter;
        }

        @Bean
        public ResourceHttpMessageConverter resourceHttpMessageConverter(){
            ResourceHttpMessageConverter resourceHttpMessageConverter = new ResourceHttpMessageConverter();
            resourceHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
            return resourceHttpMessageConverter;
        }

        private List<MediaType> getSupportedMediaTypes() {
            List<MediaType> list = new ArrayList<MediaType>();
            list.add(MediaType.IMAGE_JPEG);
            list.add(MediaType.IMAGE_PNG);
            list.add(MediaType.APPLICATION_OCTET_STREAM);
            return list;
        }

    }
于 2021-07-22T04:59:57.690 回答