[JAVA] [Spring Boot] What to do if an HttpMediaTypeNotAcceptableException occurs on an endpoint for which produces is set

Cause

The cause was that I tried to return JSON even though produces =" text / csv " was set.

Status

The situation is as follows.

What I wanted to achieve

--Return csv (set produces =" text / csv ") Set endpoint --At the time of aggregation error, the status is 400, and the error content is packed in JSON and returned.

Actually, the following annotations were added.

@PostMapping(value = "[end point]", produces = "text/csv")

What happened

--Status becomes 406 on error --The error content is also empty --On the server side, HttpMediaTypeNotAcceptableException occurs

Countermeasures

It was OK if the specification of produces was removed.

@PostMapping(value = "[end point]")

If you do not specify produces, it will be treated as good, so if there is no problem, you do not have to specify it It seems.

Supplement

If you need to specify it dynamically and explicitly, you can write the response directly using HttpServletResponse. In that case, you can write as follows.

@PostMapping(value = "/api/csv")
public void csv(HttpServletResponse response) {
    String csv = //CSV acquisition process;

    response.setHeader(
            "Content-Disposition",
            "attachment;filename=\"" + /*file name*/ + "\""
    );
    response.setCharacterEncoding("Shift-JIS");
    response.setContentType("text/csv");

    try (Writer writer = response.getWriter()) {
        writer.write(csv);
    } catch (IOException e) {
        //Error handling
    }
}

Impressions

Error handling was standardized using ʻExceptionHandler, but it took a long time to identify the cause without thinking that the producesset in the controller would penetrate that far. It was written asHttpMediaTypeNotAcceptableException` ...

Articles that I used as a reference

-What happens if you specify produces of @RequestMapping -tokuhirom's blog -To write Response data directly in Spring -Qiita

Recommended Posts

[Spring Boot] What to do if an HttpMediaTypeNotAcceptableException occurs on an endpoint for which produces is set
no space left on device What to do if an error occurs
What to do if an ActionController :: UnknownFormat error occurs
What to do if audio is not available on discordrb
What to do if FacesMessage is set but not displayed
What to do if an error occurs when nokogiri enters when bundle install
Notes on what to do when EC2 is set up with t2.micro
What to do if you should have installed Rails but an error occurs with rails -v (for beginners)
[Swift] What to do if the app icon is set but not reflected
What to do if you have installed Java for OS X on macOS
wildflly10 java8 ERROR [org.jboss.jca.core.tx.jbossts.XAResourceRecoveryImpl] (Periodic Recovery) IJ000906 What to do if an error occurs
What to do if you get an error on heroku rake db: migrate
How to set Dependency Injection (DI) for Spring Boot
What to do if TextToSpeech doesn't work on Android 11
What to do if a SAX Parser error occurs when using Liferay 7 / DXP on AWS
Steps required to issue an asynchronous event for Spring Boot
What to do if the log using JUL is no longer output to the app log after deploying the Spring Boot app to Tomcat as a war
What to do if an SSH key authentication error occurs during automatic deployment to EC2 with Capistrano
What to do if an error occurs when doing npm install axios in React + Typescript project
[Ubuntu 20.04] What to do if the external monitor is not recognized
[Rails] What to do if data is not registered in DB
What to do if Cloud9 is full in the Rails tutorial
What to do if the Eclipse Maven dependency Jar is wrong
How to set Spring Boot + PostgreSQL
What is @Autowired in Spring boot?
What to do if zip dies if there is a pom when making an executable jar with gradle
What to do when javax.batch.operations.JobStartException occurs
Spring.messages.fallback-to-system-locale: false is required to default message.properties for i18n support in Spring boot
Add gem'rails-i18n','~> 6.0.0' and what to do if bundle install gives an error
What to do if you get an error during rails db: reset
What to do if the prefix c is not bound in JSP
What to do if you get an uninitialized constant Likes Controller error
What to do and how to install when an error occurs in DXRuby 1.4.7
What to do if deployment fails on Heroku (Ruby app not detected)
What to do if tomcat process remains when tomcat is stopped in eclipse
What to do if you get an error when you hit Heroku logs
What to do if you get a MiniMagick vulnerability alert on GitHub
Notes on what to do when a WebView ClassNotFoundException occurs in JavaFX 12
What to do if you get an "A server is already running." Error when you try to start the rails server
What is a Spring Boot .original file?
What to do when a javax.el.PropertyNotWritableException occurs
What to do if you install Ubuntu
What to do if you get a SQLite3 :: BusyException: database is locked error
[Error resolution] Occurs when trying to build an environment for spring with docker
What to do if Docker Desktop fails to launch on your M1 MacBook Pro
What to do if the rails server doesn't run out on AWS cloud9
What to do if ffi installation fails when launching an application in Rails