当我尝试实现自己的映射时,我在使用 MapStruct 版本 1.4.1 时遇到问题。这是我写的代码:
package com.kucazdravlja.user.mappers;
import com.kucazdravlja.user.dto.NoticeBoardDto;
import com.kucazdravlja.user.entities.NoticeBoard;
import com.kucazdravlja.user.entities.NoticeBoardStatus;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import java.util.Objects;
@Mapper(uses = {BaseJournalMapper.class})
public interface NoticeBoardMapper {
@Mapping(source = "status", target = "status", qualifiedByName = "getNoticeBoardStatusName")
NoticeBoard dtoToEntity(NoticeBoardDto noticeBoardDto);
@Mapping(source = "status", target = "status", qualifiedByName = "getNoticeBoardStatusDescription")
NoticeBoardDto entityToDto(NoticeBoard noticeBoard);
@Named("getNoticeBoardStatusDescription")
static String getNoticeBoardStatusDescriptionConverter(NoticeBoard noticeBoard) {
return Objects.requireNonNull(NoticeBoardStatus.findByName(noticeBoard.getStatus())).getDescription();
}
@Named("getNoticeBoardStatusName")
static String getNoticeBoardStatusNameConverter(NoticeBoardDto noticeBoardDto) {
return Objects.requireNonNull(NoticeBoardStatus.findByName(noticeBoardDto.getStatus())).name();
}
}
Run Code Online (Sandbox Code Playgroud)
运行应用程序时它崩溃并给出错误
Error:(15, 5) java: Qualifier error. No method found …Run Code Online (Sandbox Code Playgroud) 关于mapStruct的问题。我有这样的情况:我从基本实体扩展类,但不确定如何映射它。这是我的案例。
基础实体:
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Long id;
}
Run Code Online (Sandbox Code Playgroud)
基数为:
public class BaseDto {
private Long id;
}
Run Code Online (Sandbox Code Playgroud)
用户实体:
public class User extends BaseEntity {
private String name;
private String lastName;
private String username;
private String password;
private String profilePicturePath;
}
Run Code Online (Sandbox Code Playgroud)
用户D至:
public class UserDto extends BaseDto {
private String name;
private String lastName;
private String username;
private String password;
private String profilePicturePath;
}
Run Code Online (Sandbox Code Playgroud)
映射器是这样的:
@Mapper(uses = {BaseMapper.class})
public interface UserMapper { …Run Code Online (Sandbox Code Playgroud) 我已经开始学习 Sleuth,但我陷入了日志配置。
\n我有这个配置:
\n <?xml version="1.0" encoding="UTF-8"?>\n<configuration>\n <include resource="org/springframework/boot/logging/logback/defaults.xml"/>\n \xe2\x80\x8b\n <springProperty scope="context" name="springAppName" source="spring.application.name"/>\n <!-- Example for logging into the build folder of your project -->\n <property name="LOG_FILE" value="C://tmp//test"/>\xe2\x80\x8b\n\n <property name="CONSOLE_LOG_PATTERN"\n value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr([${springAppName:-},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]){yellow} %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>\n\n <!-- Appender to log to console -->\n <appender name="console" class="ch.qos.logback.core.ConsoleAppender">\n <filter class="ch.qos.logback.classic.filter.ThresholdFilter">\n <level>INFO</level>\n </filter>\n <encoder>\n <pattern>${CONSOLE_LOG_PATTERN}</pattern>\n <charset>utf8</charset>\n </encoder>\n </appender>\n \xe2\x80\x8b\n <!-- Appender to log to file in a JSON format -->\n <appender name="logstash" class="ch.qos.logback.core.rolling.RollingFileAppender">\n <file>${LOG_FILE}.json</file>\n <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">\n …Run Code Online (Sandbox Code Playgroud)