Wie kann ich eine Map mit Jackson in ein gültiges JSON konvertieren?Karte nach JSON konvertieren mit Jackson
ich es tue Methode mit Googles Gson über eine Feder-Boot-REST Beitrag ...
Hier ist die RESTful Web Service:
import java.util.Map;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.Gson;
@RestController
@RequestMapping("/myservice")
public class ValidationService {
@RequestMapping(value="/validate", method = RequestMethod.POST)
public void validate(@RequestBody Map<String, Object> payload) throws Exception {
Gson gson = new Gson();
String json = gson.toJson(payload);
System.out.println(json);
}
}
Also, wenn ich es auf diese berufen mit:
curl -H "Accept: application/json" -H "Content-type: application/json" \
-X POST -d '{"name":"value"}' http://localhost:8080/myservice/validate
der Receive stdout folgende (das ist genau das, was ich will):
{"name":"value"}
Gibt es einen besseren Weg, dies mit Jackson anstelle von Googles Gson zu tun und/oder mache ich das Ganze falsch?
möglich Duplikat [Jackson Vs. Gson] (http://stackoverflow.com/questions/2378402/jackson-vs-gson) –
Neeraj Jain, Danke für den Kommentar aber: Jackson vs Gson ist über die positiven und negativen Aussagen von Jackson vs Gson. Dieser Beitrag ist eine spezifische Frage. –