Spring 4.0.x JSON/Ajax HTTP/1.1 406 Not Acceptable

让人想犯罪 __ 提交于 2019-12-02 07:34:07

For the audience.

The error was in the same URL. It contains .htm

Therefore for all the developers be sure to remove it

From

@RequestMapping(value="/findallproductobycategoria.htm", method=RequestMethod.POST,
                consumes = MediaType.APPLICATION_JSON_VALUE,
                produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Set<Producto> findAllProductoByCategoria(@RequestBody Categoria categoria){
    logger.info("findAllProductoByCategoria: {}", categoria.toString());
    return this.fakeMultipleRepository.findAllProductoByCategoria(categoria.getId());
}

To

@RequestMapping(value="/findallproductobycategoria", method=RequestMethod.POST,
                consumes = MediaType.APPLICATION_JSON_VALUE,
                produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Set<Producto> findAllProductoByCategoria(@RequestBody Categoria categoria){
    logger.info("findAllProductoByCategoria: {}", categoria.toString());
    return this.fakeMultipleRepository.findAllProductoByCategoria(categoria.getId());
}

From:

$.ajax({
    url: "/spring-utility/facturaajax/findallproductobycategoria.htm" ,
    data: JSON.stringify(json),
    dataType: 'json',
    type: "POST",

To:

$.ajax({
    url: "/spring-utility/facturaajax/findallproductobycategoria" ,
    data: JSON.stringify(json),
    dataType: 'json',
    type: "POST",

Because I have

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    Map<String,MediaType> mediaTypes = new LinkedHashMap<>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON);
    mediaTypes.put("xml", MediaType.APPLICATION_XML);
    configurer.mediaTypes(mediaTypes);
    configurer.defaultContentType(MediaType.TEXT_HTML);
}

Spring gives more preference about the URL .extension than the header content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!