想了解Spring@RestController自定义JSON反序列化器的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于jsonproperty反序列化的相关问题,此外,我们还将为您介绍关于
想了解Spring @RestController自定义JSON反序列化器的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于jsonproperty反序列化的相关问题,此外,我们还将为您介绍关于c# – 使用带有JSON.Net的自定义反序列化器反序列化JSON、Ktor自定义json对象反序列化、spring @Controller 和 @RestController 注解的区别、spring @Controller和@RestController批注之间的区别的新知识。
本文目录一览:- Spring @RestController自定义JSON反序列化器(jsonproperty反序列化)
- c# – 使用带有JSON.Net的自定义反序列化器反序列化JSON
- Ktor自定义json对象反序列化
- spring @Controller 和 @RestController 注解的区别
- spring @Controller和@RestController批注之间的区别
Spring @RestController自定义JSON反序列化器(jsonproperty反序列化)
我想对某些类使用自定义JSON反序列化器(在此处 作用 ),但无法正常工作。自定义解串器只是不被调用。
我使用Spring Boot 1.2。
解串器:
public class ModelDeserializer extends JsonDeserializer<Role> { @Override public Role deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { return null; // this is what should be called but it isn''t }}
控制器:
@RestControllerpublic class RoleController { @RequestMapping(value = "/role", method = RequestMethod.POST) public Object createRole(Role role) { // ... this is called }}
@JsonDeserialize
在角色上@JsonDeserialize(using = ModelDeserializer.class)
public class Role extends Model {
}
Jackson2ObjectMapperBuilder
Java Config中的bean@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.deserializerByType(Role.class, new ModelDeserializer());
return builder;
}
我究竟做错了什么?
编辑 可能是@RestController
由于它与@Controller
…
答案1
小编典典首先,您不需要重写Jackson2ObjectMapperBuilder
即可添加自定义反序列化器。当您无法添加@JsonDeserialize
注释时,应使用此方法。您应该使用@JsonDeserialize
或覆盖Jackson2ObjectMapperBuilder
。
缺少的是@RequestBody
注释:
@RestControllerpublic class JacksonCustomDesRestEndpoint { @RequestMapping(value = "/role", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Object createRole(@RequestBody Role role) { return role; }}@JsonDeserialize(using = RoleDeserializer.class)public class Role { // ......}public class RoleDeserializer extends JsonDeserializer<Role> { @Override public Role deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // ................. return something; }}
c# – 使用带有JSON.Net的自定义反序列化器反序列化JSON
{ "MobileSiteContents": { "au/en": [ "http://www.url1.com","http://www.url2.com",],"cn/zh": [ "http://www.url2643.com",] } }
我正在尝试将其反序列化为IEnumerable类,如下所示:
public class MobileSiteContentsContentSectionItem : ContentSectionItem { public string[] Urls { get; set; } } public abstract class ContentSectionItem { public string Culture { get; set; } }
那可能吗?
我意识到我可能需要为此使用Custom JsonConverter,但找不到任何示例.
我开始编写一个使用JObject.Parse进行转换的方法,但不确定这是否是正确/最有效的路径:
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json) { var jobject = JObject.Parse(json); var result = new List<MobileSiteContentsContentSectionItem>(); foreach (var item in jobject.Children()) { var culture = item.Path; string[] urls = new[] { "" }; //= this is the part I'm having troble with here... result.Add(new MobileSiteContentsContentSectionItem { Culture = culture,Urls = urls }); } return result; }
解决方法
>您正在迭代顶层对象的子节点,期望获得实际位于对象中的数据更低一级.您需要导航到MobileSiteContents属性的值并迭代它的子项.
>当您使用JObject的Children()时,使用允许您将它们转换为JProperty对象的重载;这将使您更容易提取所需的数据.
>从JProperty项目的名称中获取文化
>要获取网址,请获取JProperty项的值,并使用ToObject< string []>()将其转换为字符串数组.
这是更正后的代码:
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json) { var jObject = JObject.Parse(json); var result = new List<MobileSiteContentsContentSectionItem>(); foreach (var item in jObject["MobileSiteContents"].Children<JProperty>()) { var culture = item.Name; string[] urls = item.Value.ToObject<string[]>(); result.Add(new MobileSiteContentsContentSectionItem { Culture = culture,Urls = urls }); } return result; }
如果您喜欢简洁的代码,可以将其减少为“单行”:
public IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json) { return JObject.Parse(json)["MobileSiteContents"] .Children<JProperty>() .Select(prop => new MobileSiteContentsContentSectionItem { Culture = prop.Name,Urls = prop.Value.ToObject<string[]>() }) .ToList(); }
演示:
class Program { static void Main(string[] args) { string json = @" { ""MobileSiteContents"": { ""au/en"": [ ""http://www.url1.com"",""http://www.url2.com"",""cn/zh"": [ ""http://www.url2643.com"",] } }"; foreach (MobileSiteContentsContentSectionItem item in Parse(json)) { Console.WriteLine(item.Culture); foreach (string url in item.Urls) { Console.WriteLine(" " + url); } } } public static IEnumerable<MobileSiteContentsContentSectionItem> Parse(string json) { return JObject.Parse(json)["MobileSiteContents"] .Children<JProperty>() .Select(prop => new MobileSiteContentsContentSectionItem() { Culture = prop.Name,Urls = prop.Value.ToObject<string[]>() }) .ToList(); } public class MobileSiteContentsContentSectionItem : ContentSectionItem { public string[] Urls { get; set; } } public abstract class ContentSectionItem { public string Culture { get; set; } } }
输出:
au/en http://www.url1.com http://www.url2.com cn/zh http://www.url2643.com
Ktor自定义json对象反序列化
据我从 GSON 实现中得到,您需要从 JSON 反序列化
{"key1":"value1","key2":"value2",...}
进入
listOf(MyObject(member1="key1",member2="value1"),MyObject(member1="key2",member2="value2"),...)
kotlinx.serialization
也可以:
object MyObjectListSerializer : JsonTransformingSerializer<List<MyObject>>(ListSerializer(MyObject.serializer())) {
override fun transformDeserialize(element: JsonElement) =
JsonArray((element as JsonObject).entries.map { (k,v) ->
buildJsonObject {
put("member1",k)
put("member2",v)
}
})
}
用法(普通 kotlinx.serialization):
val result = Json.decodeFromString(MyObjectListSerializer,"{\"key1\":\"value1\",\"key2\":\"value2\"}")
用法(使用 Ktor 客户端):
val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(Json {
serializersModule = SerializersModule { contextual(MyObjectListSerializer) }
})
}
}
val result = client.get<List<MyObject>>("http://localhost:8000/myObj")
spring @Controller 和 @RestController 注解的区别
@Controller
弹簧和@RestController
注释之间的区别。
注释可以@Controller
同时用于 Web MVC 和 REST 应用程序吗?
如果是,我们如何区分它是 Web MVC 还是 REST 应用程序。
答案1
小编典典@Controller
用于将类标记为 Spring MVC Controller。@RestController
是一个方便的注解,它只是添加@Controller
和@ResponseBody
注解(参见:Javadoc)
所以以下两个控制器定义应该做同样的事情
@Controller@ResponseBodypublic class MyController { }@RestControllerpublic class MyRestController { }
spring @Controller和@RestController批注之间的区别
spring @Controller
和@RestController
注释之间的区别。
可以@Controller
注解同时用于Web MVC框架和REST的应用程序?
如果是,我们如何区分是Web MVC还是REST应用程序。
关于Spring @RestController自定义JSON反序列化器和jsonproperty反序列化的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c# – 使用带有JSON.Net的自定义反序列化器反序列化JSON、Ktor自定义json对象反序列化、spring @Controller 和 @RestController 注解的区别、spring @Controller和@RestController批注之间的区别等相关知识的信息别忘了在本站进行查找喔。
本文标签: