如何将@RequestBody中任意名称值对的列表映射到java对象(How to map list of arbitrary name value pair from @RequestBody to

编程入门 行业动态 更新时间:2024-10-28 06:24:06
如何将@RequestBody中任意名称值对的列表映射到java对象(How to map list of arbitrary name value pair from @RequestBody to java object)

我正在使用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) { //... }

更多推荐

本文发布于:2023-08-05 16:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1433923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   对象   名称   列表   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!