Pergunta

I am trying to get Spring 3.2 MVC to return a JSON response without the default label.

For example,

@Controller
@RequestMapping("/dt")
public class DTAgentsController {

@ModelAttribute
@RequestMapping(method = RequestMethod.GET, produces = "application/json;UTF-8")
    public DTResponse agents() {
        DTResponse resp = new DTResponse();
        resp.setsEcho(1);
        resp.setiTotalDisplayRecords(10);
        resp.setiTotalRecords(50);
        return resp;
    }
}

returns

{"DTResponse":{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10}}

I just want the JSON output to be

{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10}

Thanks.

Foi útil?

Solução

The problem is not @ModelAttribute, it just means what data that you want to store or get from model. It seems that you use jQuery datatables, so you should add @ResponseBody to the method agents().

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
    public DTResponse agents() {
        DTResponse resp = new DTResponse();
        resp.setsEcho(1);
        resp.setiTotalDisplayRecords(10);
        resp.setiTotalRecords(50);
        return resp;
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top