MapStrut 新手;对象到字符串错误:
[错误] /util/LicenseMapper.java:[11,23] 无法将属性“java.lang.Object license.customFields[].value”映射到“java.lang.String license.customFields[].value”。考虑声明/实现一个映射方法:“java.lang.String map(java.lang.Object value)”。
代码:
@Mapper
public interface LicenseMapper {
List<License> jsonToDao(List<com.integrator.vo.license.License> source);
}
Run Code Online (Sandbox Code Playgroud)
vo.license 包含具有以下属性的 CustomFields 列表
@SerializedName("Value")
@Expose
private Object value;
Run Code Online (Sandbox Code Playgroud)
Json 将一个字段作为对象输入,因为它可能是布尔值、字符串或任何其他内容,因此我已将其映射到对象中。而在 dao 层中,String 中有相同的字段。(在自定义映射器中,我只是 String.valueof 但不确定如何使用 Mapstrut 实现它)
谁能告诉我 LicenseMapper 中需要进行哪些设置才能将对象转换为字符串?
许可证结构 - 来源和目的地:
.
.
private String notes;
private Boolean isIncomplete;
private List<CustomField> customFields = null;
private List<Allocation> allocations = null;
Run Code Online (Sandbox Code Playgroud)
源中的自定义字段结构(删除了 gson 注释):
.
.
private String name;
private Object dataType;
private Object value;
Run Code Online (Sandbox Code Playgroud)
目的地中的自定义字段结构
private String name;
private …
Run Code Online (Sandbox Code Playgroud) 我有一个接受 dto 对象的控制器。我需要更改 dto 对象中存在的字段。
@PatchMapping(value = "/update/{uuid}")
public ResponseEntity<UserDto> update(
@RequestBody UserDto userDto,
@PathVariable("uuid")UUID uuid) throws UserNotFoundException {
User updatedUser = userService.update(
userMapper.userDtoToUser(userDto),
uuid
);
return .....
}
Run Code Online (Sandbox Code Playgroud)
但 userService 只能接受实体。我需要使用映射器 dto -> 实体。但是实体不能有 dto 中的空字段(假设您只需要更改一个字段)。在这种情况下该怎么办?我知道控制器不应该包含逻辑
我正在尝试创建一个 DTO 文件来转换值并保存文档。
export class CreateProductDto {
readonly pricing: {
readonly list: number;
}
}
async create(@Body() createProductDto: CreateProductDto) {
console.log(createProductDto);
console.log(createProductDto.pricing.list);
}
Run Code Online (Sandbox Code Playgroud)
import * as mongoose from 'mongoose';
export const ProductSchema = new mongoose.Schema({
pricing: {
list: {
type: Number,
},
},
});
Run Code Online (Sandbox Code Playgroud)
但 pricing.list 的值是undefined。
在 NestJS 中执行此操作的正确方法是什么?
从查询中我得到限制参数。如何转化为数字并通过Dto进行检查?
@Get('currency/:type')
getCurrency(
@Param() params: CurrencyTypeDto,
@Query('limit', ParseIntPipe) limit: number,
@Query() query: PaginationLimitDto
) {
Run Code Online (Sandbox Code Playgroud)
分页限制
export class PaginationLimitDto {
@IsOptional()
@IsInt()
limit: number;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Dozer将Hibernate实体映射到他们的DTO.简化的示例类如下:
@Entity
public class Role {
@Id
@GeneratedValue
@Column(name="RoleId", nullable=false)
public int roleId;
@Column(name="RoleName", nullable=false)
@NotEmpty
public String roleName;
//get + set (not for the roleId since its autogenerated)
}
Run Code Online (Sandbox Code Playgroud)
public class RoleDTO {
private int roleId;
private String roleName;
public RoleDTO(int roleId, String roleName) {
this.roleId = roleId;
this.roleName = roleName;
}
public RoleDTO() {}
//gets + sets
}
Run Code Online (Sandbox Code Playgroud)
现在映射工作正常,但我在尝试进行更新时遇到问题.假设我的数据库中有一个角色(1,"管理员").我的视图首先使用更新的字段生成DTO:
RoleDTO roleDTO = new RoleDTO(1, "admin");
Run Code Online (Sandbox Code Playgroud)
最终,持久化角色的类接收DTO并通过Dozer将其转换为Entity类以保留更改:
Role role = DozerMapper.map(roleDTO,Role.class);
Run Code Online (Sandbox Code Playgroud)
此时,我的角色实体已丢失其ID,可能是因为ID列被定义为自动增量,我显然无法更新null-ID实体.
那么我应该如何处理这个问题,以便ID和更新的字段全部映射到实体?我总是可以使用hibernate来实现实体对象,并使用DTO中的每个字段更新它们并将其保存回来,但它会破坏使用Dozer的整个目的.
谢谢你的帮助.
我目前在我的应用程序中手动将DTO转换为BO(反之亦然).然而,这种方法很笨拙.
这两个表示之间有没有好的映射器?
我的要求如下:
谢谢你的任何建议.
我是AutoMapper的新手.对不起,如果这太简单了.
这是我的示例域名:
我有一个篮子.它包含食物清单.食物是香蕉或泡菜.
我有DTO镜像域中的每个类.目标:从BasketDto,将其及其内容映射到篮子.
这是失败的代码.在最后一行之后,我有一个篮子,但它充满了DTO而不是常规实体:(
class Program
{
static void Main(string[] args)
{
Mapper.CreateMap<BasketDto, Basket>();
Mapper.CreateMap<PickleDto, Pickle>();
Mapper.CreateMap<BananaDto, Banana>();
var dto = new BasketDto
{
Food = new List<IFood>
{
new PickleDto { Name = "BigPickle" },
new BananaDto { Name = "SmallBanana" },
}
};
var basketFromDto = Mapper.Map<Basket>(dto);
}
}
// Domain classes and interfaces --------------
interface IFood
{
string Name { get; set; }
}
class Banana : IFood
{
public string Name { get; set; } …
Run Code Online (Sandbox Code Playgroud) 当数据库编写通过标准的应用程序为每个表,我有以下特性:CreatedOn
,CreatedBy
,ModifiedOn
,ModifiedBy
,Archived
.
但是试图跟踪DDD我在质疑这些属性是否真的是域的一部分,应该包含在域对象中.如果我要从域中排除这些"元数据"属性但仍然希望它们在我的数据库中,那么如果我打算使用ORM,我需要实现某种DTO层.
所以,领域模型映射到DTO的CreatedOn
,ModifiedOn
等设置,然后推到数据库中.
所以我想我的问题是:
我正打算为数据列表构建一个DTO.
return from p in db.Students.Find(Id).Courses
select new CourseDTO
{
Id = p.Id,
CourseName = p.CourseName
};
Run Code Online (Sandbox Code Playgroud)
但是,当我使用它时,我收到以下错误:
Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<Storage.Models.CourseDTO>' to
'System.Collections.Generic.ICollection<Storage.Models.CourseDTO>'.
An explicit conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
有谁能解释为什么?
我的一个朋友建议我在DTO中初始化类型为(ArrayList)且仅类型为ArrayList的DTO字段,以避免NullPointerException
public class fooDto {
private SomeClasse someClasse = new SomeClasse();
private ArrayList<Bar> bars = new ArrayList();
}
Run Code Online (Sandbox Code Playgroud)
我们应该做他的吗?这是一个好习惯吗
以其他方式,是否应该使用“ = new SomeClasse()”?
dto ×10
java ×5
mapper ×2
nestjs ×2
automapper ×1
c# ×1
collections ×1
dozer ×1
gson ×1
hibernate ×1
interface ×1
javascript ×1
jpa ×1
linq ×1
mapping ×1
mapstruct ×1
node.js ×1
reflection ×1
spring ×1
spring-boot ×1