- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的项目中无法使用 JsonIgnore,并且 [OpenApiIgnore] 不起作用。在 swashbuckle 中可以通过它自己的属性来制作过滤器,但是在 NSwag 中我没有找到类似的机制。
代码示例:
API 网关上的命令类:
[MessageNamespace("identity")]
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
[JsonIgnore]
public Guid Id { get; }
...
}
微服务上的命令类:
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(Guid id, string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Id = id;
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
public Guid Id { get; }
...
}
网关上的Api方法:
[HttpPut("{id}")]
[JwtAuth(Roles.Administrator)]
public async Task<ActionResult> Put(Guid id, UpdateUser command)
{
//Send command to RabbitMQ(serialized)
//Id binded before sending but after construct in service ID is missing
await SendAsync(command.Bind(c => c.Id, id));
return Accepted();
}
为什么我需要从 NSwag 生成中删除属性?因为我在路由中需要 Id,但如果 Id 也位于查询正文中,它会使我的前端编码器具有侵略性和破坏性 хD,而且它也不漂亮:
最佳答案
我的解决方案是使用单独的类
namespace API.Models
{
public class UpdateUser
{
public string Surname { get; set; }
public string Name { get; set; }
...
}
}
namespace Domain.Commands
{
public class UpdateUser:ICommand
{
[JsonConstructor]
public UpdateUser(Guid id, string surname, string name, string middleName, string department, string position, string adAccount, string email, string userName, string password)
{
Id = id;
Surname = surname;
Name = name;
MiddleName = middleName;
Department = department;
Position = position;
AdAccount = adAccount;
Email = email;
UserName = userName;
}
public Guid Id { get; }
...
}
}
在 Controller 中
namespace API.Controllers
{
public class UserController
{
[HttpPut("{id}")]
[JwtAuth(Roles.Administrator)]
public async Task<ActionResult> Put(Guid id, API.Models.UpdateUser command)
{
//Send command to RabbitMQ(serialized)
//Id binded before sending but after construct in service ID is missing
var cmd = new Domain.Commands.UpdateUser( id, command.Surname, command.Name, ... );
await SendAsync(cmd);
return Accepted();
}
}
}
Domain.Commands 可以放在类库中,也可以被 API 和微服务使用。
关于c# - 是否可以在不使用 [JsonIgnore] 的情况下忽略 nswag 中的模型属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58196772/
Field 上的 JsonIgnore 与 Jackson 字段的 getter 上的 JsonIgnore 有什么区别? 最佳答案 @JsonIgnore注解用于忽略反序列化和序列化的字段,可以直接
是否可以在序列化 JSON 响应的同时根据 If 条件排除某些元素? if(a == 1) { //show element } else { //don't show element }
我有一个域javabean,一些bean包含很多带有密码的信息并登录 Ip,我使用 @jsonIgnore 来过滤我不使用的属性希望最终用户知道。 但是有一个问题,用其他方法我使用相同的 javabe
是否可以在序列化 JSON 响应的同时根据 If 条件排除某些元素? if(a == 1) { //show element } else { //don't show element }
在处理项目时,我犯了一个错误,将 @JsonIgnore 放在 setter 而不是 getter 属性上,如下所示 private Set books; public Set getBooks()
我使用@JsonIgnore属性来忽略pojo中的一些属性,但是使用Gson库解析json后,这些字段在json响应中不会被忽略,请帮忙。 import com.fasterxml.jackson.a
我正在使用一对多基数连接表,我使用的类相互引用。而且我正在使用 @JsonIgnore 注释,但没有深入理解它。 最佳答案 @JsonIgnore 用于忽略序列化和反序列化中使用的逻辑属性。 @Jso
由于某种原因,注释@JsonIgnore在我的项目中不起作用。我已经阅读了一些答案,这可能是由于使用不同的不兼容的 Jackson 版本(org.codehaus 与 com.fasterxml)引起
我是 Java 和 Jackson 以及我尝试使用的许多其他技术的新手,所以我希望得到详细的回答。 有没有一种方法可以防止使用 Jackson 将一个或多个字段序列化为 JSON String_lik
我有以下抽象类: public abstract class StandardTimeStamp { @Temporal(TemporalType.TIMESTAMP) @Column(nul
我有一个类(class)家长 public class Parent { private int id; @JsonIgnore int getId() {} void set
我在处理 Lombok 和 Jackson 时遇到了非常奇怪的问题。以下是我正在处理的一段代码。 @Getter @Setter @NoArgsConstructor @XmlRootElement
我正在使用 spring 4.0.1、hibernate 4.3.5、jackson 1.9.2 和 STS IDE我正在创建一个以 JSON 格式返回数据的 RESTful 网络服务当我使用 Hib
我有这样的东西: @Entity @Table(name = "myEntity") public class MyEntity { //.... @Column(name = "conten
如何解决这个问题? 我尝试将 @JsonIgnore 添加到 getter,但结果是相同的。 POJO: public class Category { // Omitted details
我有一个带有 @JsonIgnore-d 字段的类: public class MyClass { ... @JsonIgnore private SomeType myfie
我有类似的东西: @Entity @Table(name = "myEntity") public class MyEntity { //.... @Column(name = "conten
我可能有 60-70 个类,它们都有各种 Id 列,当我从 Web API 返回 JSON 数据时,我想排除这些列。在内部,我加入了 Id,但任何面向前端的东西都使用 Guid。所以我的主键是 Id
JsonIgnore 属性可用于忽略序列化中的某些属性。我想知道是否可以做相反的事情?所以 JsonSerializer 会忽略每个属性,除非它有一个特殊属性? 最佳答案 是的。当您使用 [JsonO
我有两个 Java 类:Usuario 和 Entrada。 乌苏里奥: @Entity @Table(name="usuario") @NamedQuery(name="Usuario.findAl
我是一名优秀的程序员,十分优秀!