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

前端 未结 1 1565
深忆病人
深忆病人 2021-01-28 12:02

I am working with Spring 4.0.5.RELEASE, Spring MVC through only Java Config

I have in my pom.xml the following:


    &         


        
相关标签:
1条回答
  • 2021-01-28 12:27

    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

    0 讨论(0)
提交回复
热议问题