Error returning HashMap for JavaScript

3

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

  

The resource identified by this request is only capable of generating   responses with characteristics not acceptable according to the request   "accept" headers.

Calling the javascript validames () function:

    <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 the Spring MVC @RequestMapping:

@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 already and I have not found any forums to help me. Has anyone had this problem? I'm starting now with JavaWeb.

    
asked by anonymous 23.06.2016 / 03:15

1 answer

2

I believe you are returning a JSON of this action from your controller, so Spring uses Jackson to serialize its object to the JSON format, but it must 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 do not use any tools to manage your dependencies you would have to download the Jackson jar and add it to the classpath of your application, if that is your case follow the link where you can download it. >

link

- References

link

link

I hope I have helped.

    
23.06.2016 / 13:55