其余API端点

时间:2018-09-27 18:22:41

标签: java rest api http-request-parameters

为什么我不能使用 @RequestParam之后的TreeMap<String,String>吗? 当我使用

时可以使用
Map<String,String>

示例:

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

这是我看到的错误:

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]

1 个答案:

答案 0 :(得分:0)

Jackson在映射抽象类型Map时,将LinkedHashMap用作默认类型值。 您可以自定义ObjectMapper,注册自定义模块。

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