简体   繁体   English

Java_Spring:无法将“java.lang.String”类型的属性值转换为属性“paramsMap”所需的类型“java.util.Map”

[英]Java_Spring: Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'paramsMap'

Creating a rest API using Java Spring and would like to get the input in Map format, but get the below error.使用 Java Spring 创建一个 rest API 并希望以 Map 格式获取输入,但出现以下错误。 Does anyone know what is causing this exception to happen?有谁知道是什么导致了这个异常的发生?

Resources been looked (could not resolve the issue):已查看资源(无法解决问题):

http://www.java2s.com/Tutorials/Java/Spring/0140__Spring_Map_Properties.htm http://www.java2s.com/Tutorials/Java/Spring/0140__Spring_Map_Properties.htm

Spring MVC populate @RequestParam Map<String, String> Spring MVC 填充 @RequestParam Map<String, String>

https://www.baeldung.com/spring-date-parameters https://www.baeldung.com/spring-date-parameters

@RequestMapping("/api")
public interface ApiController {
          @GetMapping("/submitJob")
    JobSubmitResultDTO submitJob(@RequestBody JobSubmitRequestDTO request);
}

@Data
public  class JobSubmitRequestDTO {

    Map<String,Object> paramsMap = new HashMap<String,Object>();

}

@Data
public class JobSubmitResultDTO {
    String jobID;
    String message;
}  

@RestController
public class ApiController implements ApiController {


    @Override
    public JobSubmitResultDTO submitJob(JobSubmitRequestDTO request) {

        //Create an instance of JobSubmitResultDTO
        JobSubmitResultDTO response = new JobSubmitResultDTO();
        return response; //return null values for now 

    }
}

URL:  http://XXXXXXXXX:XXXXX/api/submitJob?paramsMap=%7B%7D

***passing {} -> %7B%7D or {'Age':12} would return the same error*** 

Error:
    {
  "timestamp": "2020-03-19T21:38:15.443+0000",
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.validation.BindException",
  "errors": [
    {
      "codes": [
        "typeMismatch.jobSubmitRequestDTO.paramsMap",
        "typeMismatch.paramsMap",
        "typeMismatch.java.util.Map",
        "typeMismatch"
      ],
      "arguments": [
        {
          "codes": [
            "jobSubmitRequestDTO.paramsMap",
            "paramsMap"
          ],
          "arguments": null,
          "defaultMessage": "paramsMap",
          "code": "paramsMap"
        }
      ],
      "defaultMessage": "Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'paramsMap'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map' for property 'paramsMap': no matching editors or conversion strategy found",
      "objectName": "jobSubmitRequestDTO",
      "field": "paramsMap",
      "rejectedValue": "{}",
      "bindingFailure": true,
      "code": "typeMismatch"
    }
  ],
  "message": "Validation failed for object='jobSubmitRequestDTO'. Error count: 1",
  "path": "/api/submitJob"
}

You are accepting data in Request Body using code.您正在使用代码接受请求正文中的数据。

JobSubmitResultDTO submitJob(@RequestBody JobSubmitRequestDTO request); 

Send your JobSubmitRequestDTO object into request body.将您的 JobSubmitRequestDTO 对象发送到请求正文中。 Here is sample request body of JSON type that is working (I tested using postman) :这是 JSON 类型的示例请求正文,它正在工作(我使用邮递员进行了测试):

{ "paramsMap": { "Key1":"Value1", "Key2":"Value2" } } { "paramsMap": { "Key1":"Value1", "Key2":"Value2" } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [无法将类型&#39;java.lang.String []&#39;的属性值转换为属性的必需类型&#39;java.util.List&#39; - [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 无法将类型&#39;java.lang.String&#39;的属性值转换为必需类型&#39;java.util.Date&#39; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' 无法将 java.lang.String[] 类型的属性值转换为所需的类型 java.util.List - Failed to convert property value of type java.lang.String[] to required type java.util.List 无法将 java.lang.String 类型的属性值转换为所需类型 java.util.Date - Failed to convert property value of type java.lang.String to required type java.util.Date 无法将属性“ dateOfBirth”的类型“ java.lang.String”的属性值转换为所需的类型“ java.util.Date”; - Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateOfBirth'; 错误:无法将类型java.lang.String的属性值转换为属性dob的必需类型java.util.Date? - Error: Failed to convert property value of type java.lang.String to required type java.util.Date for property dob? 无法将属性fromDate的java.lang.String类型的属性值转换为所需的java.util.Date类型; - Failed to convert property value of type java.lang.String to required type java.util.Date for property fromDate; 无法将“java.lang.String”类型的属性值转换为属性“transactions”所需的类型“java.util.List” - Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'transactions' 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”) Spring MVC - 无法将“java.lang.String”类型的属性值转换为所需类型“java.lang.Integer” - Spring MVC - Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM