In the development of Java engineering, I believe you have also encountered interface request body is too complex, with objects to map, layer upon layer nesting, not conducive to construction, not also conducive to maintenance, very troublesome situation, like this:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class AmRequestDTO {
private Session session = new Session();
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Session {
private String session_id;
private Integer processed_duration;
private Long timestamp;
private Integer wanted_sequence_id;
private String message;
private List<Sentence> sentence;
private Integer slice_time;
private Boolean is_silence;
private Float valid_speech_ratio;
privateJSONObject extend; .@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Sentence {
private Integer sentence_id;
private String post_processed_text;
private Float dirty_words_probability;
privateList<Words> words; .@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Words {
private Integer end_time;
private String text;
privateFloat confidence; }}}}Copy the code
Constructing such request bodies is a disaster for Java developers. (No camel name to facilitate mapping fields in the RequestBody)
Stringutil. replace(CharSequence, Map<String, V>)
StringUtil here is not common in Commons, see POM:
<dependency>
<groupId>com.github.ifeilong</groupId>
<artifactId>feilong</artifactId>
<version>3.0.10</version>
</dependency>
Copy the code
For your convenience:
- Feilong-core makes Java development easier: feilong-core.mydoc. IO /
- Yards cloud: gitee.com/ifeilong/fe…
- Methods: venusdrogon. Making. IO/feilong – PLA…
2. Use examples
Taking the RequestBody of ES as an example, focus only on the main parts here:
{
"filters": {
"and": [{"field": "floor_level_recall"."action": "term"."value": "${floorLevel}"
},
{
"field": "building_age_lower"."action": "lte"."value": ${buildingFinishYear}
},
{
"field": "build_area_lower"."action": "lte"."value": ${houseArea}
}
]
}
}
Copy the code
Use stringutil.replace (CharSequence, Map<String, V>) as a template to parse the matching variables with the String given above:
@Test
public void testStringUtil(a) {
// Base string
String strBenchmark = "{\"filters\":{\"and\":[{\"field\":\"floor_level_recall\",\"action\":\"term\",\"value\":\"${floorLevel}\"}," +
"{\"field\":\"building_age_lower\",\"action\":\"lte\",\"value\":${buildingFinishYear}}," +
"{\"field\":\"build_area_lower\",\"action\":\"lte\",\"value\":${houseArea}}]}}";
// The value to match
Map<String, String> map = new HashMap<>(3);
map.put("floorLevel", String.valueOf(19));
map.put("buildingFinishYear", String.valueOf(2015));
map.put("houseArea", String.valueOf(119.5));
System.out.println(StringUtil.replace(strBenchmark, map));
}
Copy the code
Output (after formatting) :
{
"filters": {
"and": [{"field": "floor_level_recall"."action": "term"."value": "19"
},
{
"field": "building_age_lower"."action": "lte"."value": 2015
},
{
"field": "build_area_lower"."action": "lte"."value": 119.5}}}]Copy the code
ToJSONString () : fastJSON; toJSONString() : fastjson; toJSONString(); A final field or entire structure is used as a String or JSON. The above is a personal solution, welcome to share better solutions, together write more elegant, concise code!