Error returning Hashmap to Javascript

Asked

Viewed 88 times

3

When returning the hashmap to javascript I got the following error:

The Resource identified by this request is only capable of generating Sponses with Characteristics not acceptable According to the request "Accept" headers.

Javascript validatable() function call:

    <div data-role="controlgroup">
        <label for="txtMesa">Informe a mesa de origem:</label>
        <input type="text" name="codigo" id="txtMesa" maxlength="3" onchange="validamesa()"/>
    </div>

Validate function in js file:

function validamesa(){
    var mesa = document.getElementById("txtMesa").value;

    if (mesa != null || mesa != "")
    {
        $.ajax( {
            type : "GET",
            url : "validarMesa.do?mesa="+mesa,
            success : function(data) {
                if (data.mensagem != "OK"){ 
                    alert(data.mensagem);
                    document.getElementById("txtMesa").value = "";
                    document.getElementById("txtMesa").focus();
                }
            }
            error : function(msg) {
                                console.log(msg.responseText);
                alert("Erro:"+msg.responseText);
            }
        });
    }
}

Now @Requestmapping of Spring MVC:

@RequestMapping("/validarMesa.do")
public @ResponseBody 
Map<String, ? extends Object> validarMesa(BigDecimal mesa,
        HttpSession session, HttpServletRequest req,
        HttpServletResponse resp) {
    Map<String, Object> modelMap = null;
    Tab_MesasDao oMesaDao = null;

    try {
        oMesaDao = new Tab_MesasDao();
        if (oMesaDao.validar_Mesa(mesa)) {
            modelMap = new HashMap<String, Object>(1);
            modelMap.put("mensagem", "OK");
            resp.setStatus(200);
        } else {
            modelMap = new HashMap<String, Object>(1);
            modelMap.put("mensagem", "ERRO!" + oMesaDao.getMensagem());
            resp.setStatus(200);
        }
    } catch (Exception ex) {
        modelMap = new HashMap<String, Object>(1);
        modelMap.put("mensagem", "ERRO!" + ex.getMessage());
        resp.setStatus(404);

    } finally {
        oMesaDao = null;
    }

    return modelMap;
}

I’ve been having this problem for a few days now and haven’t found any forums to help me. Has anyone ever had this problem? I’m starting now with Javaweb.

  • The problem seems simple: your AJAX request does not say what kind of return it expects in the header Accept and similarly you do not specify in the method what kind of return. How will Java guess that you want JSON and not XML, for example? You need to either set the type in the method or use the attribute accepts in the AJAX call.

1 answer

2


I believe that you are returning a JSON of this action from your controller, so Spring uses Jackson to serialize your object to the JSON format, however the same has to be configured in your application if your application uses Maven would be something as simple as adding Jackson to its dependencies, something like:

<dependencies>
...
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.2.3</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.2.3</version>
  </dependency>
  ...
</dependencies>

Now if you don’t use any tool to manage your dependencies you would download the Jackson jar and add it in the classpath of your application, if that is your case follow the link where you can download the same.

http://wiki.fasterxml.com/JacksonDownload

-- References

http://wiki.fasterxml.com/JacksonHome

https://github.com/FasterXML/jackson

I hope I’ve helped.

  • Beauty Leonardo, just download the jar of Jackson and add it to my project already solved my problem. Thank you so much.

  • Good Matheus, :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.