我正在使用Spring boot mvc来构建REST服务。 PUT输入数据就像
[ { "PERSON":"John" }, { "PLACE":"DC" }, { "PERSON":"John" }, { "PERSON":"Joe" }, { "RANDOM NAME 011":"random string" }, { "OTHER RANDOM NAME":"John" } ]请注意,名称部分是任意的,值部分也是如此。 如何使用spring的自动转换转换它? 我的代码就像
@RequestMapping(value = "/cleansing", method = RequestMethod.PUT) public ResponseEntity<Void> cleanse(@RequestBody List<CategoryItem> data) {我知道@RequestBody List<CategoryItem> data部分不对,我不知道如何编写CategoryItem类使其工作。 什么是正确的选择?
I am using Spring boot mvc to build a REST service. The PUT input data are like
[ { "PERSON":"John" }, { "PLACE":"DC" }, { "PERSON":"John" }, { "PERSON":"Joe" }, { "RANDOM NAME 011":"random string" }, { "OTHER RANDOM NAME":"John" } ]Notice that the name part is arbitrary, so is the value part. How to convert it using spring's automatic conversion? My code is like
@RequestMapping(value = "/cleansing", method = RequestMethod.PUT) public ResponseEntity<Void> cleanse(@RequestBody List<CategoryItem> data) {I know @RequestBody List<CategoryItem> data part is not right, I don't know how to write CategoryItem class to make it work. What is aright alternative?
最满意答案
你想要它的Object是什么?
正如HTTP PUT有效负载一样,您的签名将作为List<Map<String, String>> ,但诚实地说这有点笨拙。 考虑到您的密钥是随机的,检索值会特别麻烦。 这样的东西可用于处理整个数据集:
@RequestMapping(value = "/cleansing", method = RequestMethod.PUT) public ResponseEntity<Void> cleanse(@RequestBody List<Map<String, String>> data) { for(final Map<String, String> map : data) { for(final Map.Entry<String, String> e : map.entrySet()) { System.out.println("Key: " + e.getKey() + " :: Value: " + e.getValue()); } } }就像我说的那样......有点笨重。
更灵活的方法是使用Jackson JsonDeserializer以及自定义类:
public class MyClassDeserializer extends JsonDeserializer<MyClass> { public abstract MyClass deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { //... } } @JsonDeserialize(using=MyClassDeserializer.class) public class MyClass { //... }然后在控制器方法中使用它:
@RequestMapping(value = "/cleansing", method = RequestMethod.PUT) public ResponseEntity<Void> cleanse(@RequestBody MyClass data) { //... }What Object do you want it to be?
As the HTTP PUT payload is, your signature would work as a List<Map<String, String>>, but that is a bit clunky to be honest. To retrieve values would be especially cumbersome, considering your keys are random. Something like this would work for processing the whole set of data:
@RequestMapping(value = "/cleansing", method = RequestMethod.PUT) public ResponseEntity<Void> cleanse(@RequestBody List<Map<String, String>> data) { for(final Map<String, String> map : data) { for(final Map.Entry<String, String> e : map.entrySet()) { System.out.println("Key: " + e.getKey() + " :: Value: " + e.getValue()); } } }Like I was saying...a little bit clunky.
A more flexible approach would be to use a Jackson JsonDeserializer, along with a custom class:
public class MyClassDeserializer extends JsonDeserializer<MyClass> { public abstract MyClass deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { //... } } @JsonDeserialize(using=MyClassDeserializer.class) public class MyClass { //... }And then using that in your controller method:
@RequestMapping(value = "/cleansing", method = RequestMethod.PUT) public ResponseEntity<Void> cleanse(@RequestBody MyClass data) { //... }更多推荐
发布评论