我已设置其中包含字符串["a" , "b" , "c"]
,我想POST
json数据,例如(逗号分隔和一个字符串)这是
JSON
{"view" : "a,b,c",
"fruits" : "apple"}
使用Resttemplate的postForObject方法进行终结处理?我使用了GSON
,但是在我的项目中不起作用。还有其他选择吗?
这是我的代码
private run(set<data> datas) {
Set<string> stack = new hashset<>();
iterator<data> itr = datas.iterator();
while (itr.hasnext()) {
data macro = itr.next();
if (//some condition) {
stack.add(macro);
}
}
}
}
Resttemplate.getmessageconverters().add(stringconvertor);
String result = resttemplate.postforobject(endpoint, request, String.class);
}
如果数据采用特定的类格式,则可以使用Spring Boot鼓励的POJO方法。但是看看您的示例,您似乎想要实现一次JSON Object响应。
import org.json.simple.JSONObject;
public static void run(set<data> datas, string endpoint){
// build your 'stack' set
String joined = String.join(",", stack);
JSONObject obj=new JSONObject();
obj.put("view",joined);
obj.put("fruits","apple");
//return the jsonObject as the response to your entrypoint using your method
}
[如果您在Spring Boot中使用@ResponseBody批注,也可以将以下内容转换为适当的(JSON)格式。
HashMap<String, String> map = new HashMap<>();
map.put("view", joined);
map.put("fruits", "apple");
return map;