如何使用Jackson将Map转换为有效的JSON?
我正在使用Google的GSON通过Spring Boot REST Post方法进行转换...
以下是RESTful Web服务:
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);
}
}
所以,当我使用以下方式调用它时:
curl -H "Accept: application/json" -H "Content-type: application/json" \
-X POST -d '{"name":"value"}' http://localhost:8080/myservice/validate
接收以下内容并输出到标准输出(这正是我想要的):
{"name":"value"}
使用Jackson而不是Google的Gson会有更好的方法吗,或者我完全走错了吗?