0

Hi i am trying to make an API call from another legacy application. I cannot add any more dependency in legacy application. The legacy application is trying to make a call to API with request Body as argumentsMap.toString() , here argumentsMap is a HashMap which contains lot of parameters . I cannot add any more dependency in legacy app like ObjectMapper. Currently i am trying to receive it as below.

public byte[] report(@RequestBody String argumentsMapParams) {
  ObjectMapper objectMapper = new ObjectMapper();
  String jsonFormat = objectMapper.writeValueAsString(stringReportArguments);
  ReportArguments reportArguments = objectMapper.readValue(jsonFormat,ReportArguments.class);
}

My aim is to convert the string i am receiving into ReportArguments class. Above code giving me an error like

no String-argument constructor/factory method to deserialize from String value

How i can do it .

The parameter i am receiving will be like , plain text

 {fromDate=12, rptSql=select * from property}

This is my ReportArguments Class

 @Data
 @NoArgsConstructor
 @AllArgsConstructor
 @ToString
 @JsonIgnoreProperties
 public class ReportArguments implements Serializable {

 @JsonProperty("fromDate")
 String fromDate;
 @JsonProperty("rptSql")
 String rptSql;
VKP
  • 589
  • 1
  • 8
  • 34
  • Can you add your ReportArguments class? – Vitaly Chura Sep 21 '22 at 14:39
  • Updated the question with the class – VKP Sep 21 '22 at 14:41
  • Have you tried simply using `byte[] report(@RequestBody ReportArguments reportArguments)` directly? – sp00m Sep 21 '22 at 14:59
  • 1
    Wait, you're receiving strictly this: `{fromDate=12, rptSql=select * from property}`? This isn't valid JSON, not is it any known format (to me at least), so I'm afraid you'll have to parse it manually. This data "JSONised" would be `{"fromDate": 12, "rptSql": "select * from property"}`, which Spring via Jackson/GSON/AnyJsonParser would be able to parse. – sp00m Sep 21 '22 at 15:02
  • Correct , This is coming as a plain text . i believe that is the problem here . How i can convert it to json string – VKP Sep 21 '22 at 15:03
  • This worth a try https://stackoverflow.com/questions/26485964/how-to-convert-string-into-hashmap-in-java, but since your data contains sql, may need special handling. – samabcde Sep 21 '22 at 15:25

1 Answers1

0

Have you included getter/setter method for ReportArguments. Object mapper will serialize/deserialize based on getter/setter method.

Sisir Ashik
  • 49
  • 2
  • 10
  • I think the problem is with the plain string to json string conversion. I updated the question with my class as well . The class have getters and setters – VKP Sep 21 '22 at 14:42