javaspringspring-mvcmedia-typehttp-accept-header

httpmediatypenotacceptableexception spring mvc


I am trying to return an image as shown here https://www.baeldung.com/spring-mvc-image-media-data

All methods that are shown there are working properly, except for the last one.

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

or, if we want more control over the response 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);
    }

When using this method, I get an error: The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.

I tried many options and none of them helped. I studied these pages

HTTP Status 406 – Not Acceptable in spring MVC

"Could not find acceptable representation" using spring-boot-starter-web

Could not find acceptable representation

and many others and none of the answers on these pages helped solve the problem.

If I do:

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

then I get the error: No converter for [class org.springframework.web.context.support.ServletContextResource] with preset Content-Type 'null'

If I do:

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

then I get the error: The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.

This is my github project https://github.com/MyTestPerson/images

Please tell me what am I doing wrong?

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>

Solution

  • Thanks to @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;
            }
    
        }