asgallant
asgallant

Reputation: 26330

Spring MVC error: org.springframework.web.HttpMediaTypeNotAcceptableException

I am working on a controller that serves up a CSV download of a data pull to my users, and I have an object, controller, and HttpMessageConverter based on the wonderfully helpful post here: https://stackoverflow.com/a/9961748/613559

My CsvResponse object:

public class CsvResponse {    
    private final String filename;
    private final List<Object[]> records;
    private final List<String> columns;

    public CsvResponse(List<Object[]> records, List<String> columns, String filename) {
        this.records = records;
        this.filename = filename;
        this.columns = columns;
    }
    public String getFilename() {
        return filename;
    }
    public List<Object[]> getRecords() {
        return records;
    }
    public List<String> getColumns() {
        return columns;
    }
}

My controller:

@RequestMapping(value="/download", method = RequestMethod.POST, produces = "text/csv")
public @ResponseBody CsvResponse download (@RequestParam("jsonData") String jsonData) throws IOException {
    // populate dataset and columns, removed for simplicity

    return new CsvResponse(dataset, columns, "download_" + new SimpleDateFormat("yyyy.MM.dd_HH:mm:ss").format(new Date()) + ".csv");
}

My HttpMessageConverter:

public class CsvMessageConverter extends AbstractHttpMessageConverter<CsvResponse> {
    public static final MediaType MEDIA_TYPE = new MediaType("text", "csv", Charset.forName("utf-8"));
    public CsvMessageConverter() {
        super(MEDIA_TYPE);
    }

    protected boolean supports(Class<?> clazz) {
        return CsvResponse.class.equals(clazz);
    }

    protected void writeInternal(CsvResponse response, HttpOutputMessage output) throws IOException, HttpMessageNotWritableException {
        OutputStream out;
        CsvWriter writer;
        List<Object[]> dataset;

        output.getHeaders().setContentType(MEDIA_TYPE);
        output.getHeaders().set("Content-Disposition", "attachment; filename=\"" + response.getFilename() + "\"");
        out = output.getBody();
        writer = new CsvWriter(new OutputStreamWriter(out), '\u0009');
        dataset = response.getRecords();
        for(Object[] datasetRow : dataset) {
            for(Object cell : datasetRow) {
                writer.write((cell instanceof String) ? "\"" + cell.toString() + "\"" : cell.toString());
            }
            writer.endRecord();
        }
        writer.flush();
        writer.close();
    }

    @Override
    protected CsvResponse readInternal(Class<? extends CsvResponse> type, HttpInputMessage him) throws IOException, HttpMessageNotReadableException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

When I access the download controller, I get the error message org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation. Debugging reveals that the CsvMessageConverter is not being accessed. If I remove the produces = "text/csv" segment from the controller, I get a JSON string representation of the CsvResponse object (as is correct for the @ResponseBody annotation), so the rest of the code is working properly. I figure I'm just missing something really simple here, but after banging my head against the wall all day, I still can't see it. What do I need to change to get the server to use the CsvMessageConverter?

Upvotes: 0

Views: 3333

Answers (1)

Larry.Z
Larry.Z

Reputation: 3724

You should register your csv converter by adding this block to spring-mvc.xml

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="com.xxx.utils.CsvMessageConverter"></bean>
    </mvc:message-converters>
</mvc:annotation-driven>

I have written an example and it works.

Upvotes: 1

Related Questions