简体   繁体   中英

Spring/Jackson RequestParam with HashMap from Angular

Trying to understand what is wrong with the sending of a Hashmap of values.

Client-side (Angular)

var params = {
    year: reportYear,
    reportAgg: {
        "interest": "Java",
        "domain": "JavaCodeGeeks.com"
    }
};

return $http.post('test/' + pathVariable1 + '/',
    $.param(params), {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });

Server-Side (Spring/Jackson)

@RequestMapping(value = RqMapping, method = RequestMethod.POST)
public ResponseEntity sendReports(
    final @PathVariable(value = "pathVariable1") String organizationId,
    final @RequestParam(value = "year", required = true) int requestedYear,
    final @RequestParam(value = "reportAgg", required = false) Map<String, String> reportAgg,
    final HttpServletRequest request) {

In the request.getParametersMap its possible to see the expected reportAgg[interest] and reportAgg[domain] but still i get a null value for reportAgg in the mapping.

You can use JackSon's ObjectMapper to convert into HashMap

 @RequestMapping(value = RqMapping, method = RequestMethod.POST)
    public ResponseEntity sendReports(
        final @PathVariable(value = "pathVariable1") String organizationId,
        final @RequestParam(value = "year", required = true) int requestedYear,
        final @RequestParam(value = "reportAgg", required = false) String reportAgg,
        final HttpServletRequest request) {
                ObjectMapper mapper = new ObjectMapper();
                String json = reportAgg;

                Map<String, String> map = new HashMap<String, String>();

                // convert JSON string to Map
                map = mapper.readValue(json, new TypeReference<Map<String, String>>(){});

                System.out.println(map ); // out put as HashMap<String, String>

}// end of sendReports()

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