httpmediatypenotacceptableexception spring mvc

httpmediatypenotacceptableexception spring mvc

我正在尝试 return 此处显示的图像 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");
    }

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

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

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

我尝试了很多选项,其中 none 有帮助。我研究了这些页面

和许多其他人以及这些页面上的 none 个答案帮助解决了问题。

如果我这样做:

    @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

请告诉我我做错了什么?

Image.class

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

感谢@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;
        }

    }