不支持无参数构造函数的引用类型的反序列化 [英] Deserialization of reference types without parameterless constructor is not supported

查看:550
本文介绍了不支持无参数构造函数的引用类型的反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个API

 public ActionResult AddDocument([FromBody]AddDocumentRequestModel documentRequestModel)
        {
            AddDocumentStatus documentState = _documentService.AddDocument(documentRequestModel, DocumentType.OutgoingPosShipment);
            if (documentState.IsSuccess)
                return Ok();

            return BadRequest();
        }

这是我的请求模型

    public class AddDocumentRequestModel
    {
        public AddDocumentRequestModel(int partnerId, List<ProductRequestModel> products)
        {
            PartnerId = partnerId;
            Products = products;
        }

        [Range(1, int.MaxValue, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
        public int PartnerId { get; private set; }

        [Required, MustHaveOneElement(ErrorMessage = "At least one product is required")]
        public List<ProductRequestModel> Products { get; private set; }
    }

因此,当我尝试通过此主体访问API

so when I'm trying to hit the API with this body

{
        "partnerId": 101,
        "products": [{
            "productId": 100,
            "unitOfMeasureId": 102,
            "quantity":5
        }
     ]
}

这是请求:System.NotSupportedException:不支持没有无参数构造函数的引用类型的反序列化.键入"Alati.Commerce.Sync.Api.Controllers.AddDocumentRequestModel"

this is the request : System.NotSupportedException: Deserialization of reference types without parameterless constructor is not supported. Type 'Alati.Commerce.Sync.Api.Controllers.AddDocumentRequestModel'

我不需要无参数构造函数,因为它不读取主体参数.是否还有其他反序列化方法?

I don't need parameterless constructor,because it doesn't read the body parameters.Is there any other way for deserialization?

推荐答案

您可以实现所需的结果.您需要切换到NewtonsoftJson序列化(来自Microsoft.AspNetCore.Mvc.NewtonsoftJson包)

You can achieve your desired result. You need to switch to NewtonsoftJson serialization (from package Microsoft.AspNetCore.Mvc.NewtonsoftJson)

在Startup.cs中使用ConfigureServices方法调用此函数:

Call this in Startup.cs in the ConfigureServices method:

    services.AddControllers().AddNewtonsoftJson();

此后,将通过反序列化调用您的构造函数.

After this, your constructor will be called by deserialization.

其他信息:我正在使用ASP Net Core 3.1

Extra info: I am using ASP Net Core 3.1

后来的我想提供更多有关此的信息,因为这似乎也可以通过使用System.Text.Json来实现,尽管自定义实现是必需的.来自颌骨的答案指出

Later I wanted to give more info on this, as it seems that this can also be achieved by using System.Text.Json, although custom implementation is necessary. The answer from jawa states that Deserializing to immutable classes and structs can be achieved with System.Text.Json, by creating a custom converter (inherit from JsonConverter) and registering it to the converters collection (JsonSerializerOptions.Converters) like so:

   public class ImmutablePointConverter : JsonConverter<ImmutablePoint>
   {
   ...
   }

然后...

   var serializeOptions = new JsonSerializerOptions();
   serializeOptions.Converters.Add(new ImmutablePointConverter());
   serializeOptions.WriteIndented = true;

这篇关于不支持无参数构造函数的引用类型的反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆