2

I am having a real tough time with this. I have done a ton a research and nothing has worked. Please help. I have a spring REST call and a jquery ajax PUT and POST method that both give a 400 bad request. I have done JSON.stringify, raw object data, everything. I can't seem to find what is wrong and the ONLY clue I have is one 400 bad request error with no stack trace, nothing about how it SHOULD be formatted...nothing. Is there a way I can get more information as to what is wrong? It does not even get to the first line in the spring REST method below.

Spring REST code:

@RequestMapping(value = "/supervisor/agent", method=RequestMethod.PUT)
public void updateAgent(@RequestBody AgentDTO agent)
{
    try {
        AgentDTO sessionAgent = (AgentDTO) session.getAttribute(ADAuthenticationSuccessHandler.SESSION_AGENT);
        RestTemplate restTemplate = new RestTemplate();
        log.debug("Supervisor updating agent:"+agent);
        String allURL = acrURL+"/company/"+sessionAgent.getCompanyGuid()+"/supervisor/"+sessionAgent.getAgentGuid()+"/agent/"+agent.getAgentGuid();
        log.debug("Supervisor updating agent url:"+allURL);
        agent.setEnabled(Mytime.ENABLED);
        restTemplate.put(allURL, agent);
        log.debug("Supervisor Agent updated");
    } catch (Exception e) {
        log.error("Error supervisor updating agent");
        e.printStackTrace();
    }
}

Here is the JQuery ajax call:

function editAgent()
{
    console.log("edit agent");
    console.log("chosenAgent:"+chosenAgent);
    var anAgent = myAgents[chosenAgent];
    anAgent.firstName = $('#SDEAFirstName').val();
    anAgent.lastName = $('#SDEALastName').val();
    anAgent.addressEmail = $('#SDEAEmail').val();
    console.log(anAgent);
    console.log(anAgent.agentGuid);
//  var testData = '{"addressEmail": "agent7@csi.com","agentGuid": "EC165F8A-28F4-4765-BDC5-893722FCF6AA","firstName": "Agent","lastName": "0071","workStatus": "Offline}';
    $.ajax({
        type : 'PUT',
        url : "/mytime.agentdesktop/supervisor/agent",
        contentType : "application/json; charset=utf-8",
        data: JSON.stringify(anAgent),
        dataType : 'json',
        success : function(result)
        {
            console.log("Agent edited:",anAgent.agentGuid);
            init = false; // needed to reload agent data. Otherwise, it just grabs it but doesn't update gui
            getMyAgents(); // reload agent data now that this agent was deleted
        },
        error : function(jqXHR, textStatus, errorThrown)
        {
            console.error("editAgent:status:"+textStatus+" error:", errorThrown);
        }
    });
    editAgentDialog.dialog('close');
}
user3594045
  • 33
  • 1
  • 9
  • 1
    Hard to answer without knowing how `AgentDTO` looks like. You could try setting Spring MVC and/or Jackson log level to `TRACE`. Could it be the `enabled` field? Your Java code manipulates it but there's no mention of it in the JS counterpart. The Spring controller is not being called because the error happens during parameter binding phase (i.e. when Jackson turns JSON string into an `AgentDTO` instance). – kryger Jun 29 '14 at 22:37
  • 1
    Print `JSON.stringify(anAgent)` on the console and compare it with with the structure of `AgentDTO`. The field names and data types should match. – Nikhil Talreja Jun 30 '14 at 03:04

1 Answers1

4
@RequestMapping(value = "/supervisor/agent", method=RequestMethod.PUT, consumes = "application/json")

You need to state which type of data you would be receiving in controller.

consumes = "application/json" does this.