Using this answer as a hint, I developed a Spring Boot controller for /greetings
to return greeting in different languages in JSON.
While I am getting the output in the format (array of objects) I wanted, can you please let me know if there is a better way?
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
@RestController
public class GreetingController {
@GetMapping("/greetings")
public HashMap<String, Object> getGreeting() {
ArrayList<Object> al = new ArrayList<Object>();
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("en", "Greetings!");
al.add(map1);
HashMap<String, String> map2 = new HashMap<>();
map2.put("hi", "Namaste!");
al.add(map2);
//
HashMap<String, Object> finalMap = new HashMap<>();
finalMap.put("all", al);
return finalMap;
}
}
Received (valid) output:
{
"all": [
{
"en": "Greetings!"
},
{
"hi": "Namaste!"
}
]
}
>` part.