0

Why can't I use TreeMap<String,String> after @RequestParam ? It works when I use

Map<String,String>

Example:

 @RequestMapping(value = "customers/new", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<String> createAuthorizationRole(
        @RequestParam TreeMap<String, String> hierarchy,
        @RequestHeader(name = "userid") String userid,
        @RequestHeader(name = "clientid") String clientid,
        @RequestHeader(name = "authorization") String authToken) {

        return new ResponseEntity<>("new Customer created", HttpStatus.OK);
    }

This is the Error I see:

java.lang.IllegalStateException: argument type mismatch
HandlerMethod details: 
Controller [com.demo.customer.odr.pc.branchAuthorization.BranchAuthorizationController]
Method [public org.springframework.http.ResponseEntity<java.lang.String> com.walmart.replenishment.odr.pc.branchAuthorization.BranchAuthorizationController.createAuthorizationRole(java.util.TreeMap<java.lang.String, java.lang.String>,java.lang.String,java.lang.String,java.lang.String)]
Resolved arguments: 
[0] [type=java.util.LinkedHashMap] [value={country=US, division=1, department=17, category=9295, subcategory=21032, subclass=0, fineline=9379}]
[1] [type=java.lang.String] [value=office\r0sss]
[2] [type=java.lang.String] [value=05af7587-2209-49e3-9193-64828ad240b3]
[3] [type=java.lang.String] [value=dhfdrNGXU7j19DZb]
James Kristen
  • 75
  • 1
  • 3
  • 12
  • Please show the error. – OldProgrammer Sep 27 '18 at 18:28
  • But why you want to return a implemented type and not its generalization/interface? I think it is the default spring behaviour, its philosofy is "depends on abstractions and not implementations", its a dependency injection principle. I really don't know why it does not work, but I think it is a good thing, no? :-) – lpFranz Sep 27 '18 at 18:30
  • What if I need to save the similar order that customers enters. Using treemap will save the values according to the natural order of the keys – James Kristen Sep 27 '18 at 18:34
  • @OldProgrammer added error. Thanks – James Kristen Sep 27 '18 at 18:39
  • @IpFranz Also , I dont want customers entering null value to hierarchy. Since TreeMap does not allow null values, I am trying to use TreeMap in specific – James Kristen Sep 27 '18 at 18:42
  • Your hierarchy seems to have sensitive data. I would suggest to send it in the body (using @RequestBody) rather than sending it in the URL. – Adina Rolea Sep 27 '18 at 19:23

1 Answers1

0

Jackson uses LinkedHashMap as default type value when maps the abstract type Map. You can customize your ObjectMapper, registering a custom module.

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("custom", Version.unknownVersion());
module.addAbstractTypeMapping(Map.class, TreeMap.class);
mapper.registerModule(module);