简体   繁体   中英

Spring 3.2 Web MVC @ModelAttribute without label

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM