小编Geo*_*vov的帖子

如何一起使用mapstruct和springboot bean?@自动连线

@Mapper(componentModel = "spring")
public interface DemoConvert {
    public static DemoConvert INSTANCE = mappers.getMapper(DemoConvert.class);

    @AutoWired
    private PersonInfoSearchService personInfoSearchService;

    @Mapping(source = "name", target = "name")
    @Mapping(source = "id", target = "gender", expression = "java(personInfoSearchService.searchGenderById(id))")
    PersonDTO toPerson(TeacherDTO teacherDTO);
}
Run Code Online (Sandbox Code Playgroud)

如何一起使用mapstruct和springboot bean?@自动连线

java javabeans autowired spring-boot mapstruct

8
推荐指数
1
解决办法
1万
查看次数

Junit5 从 @MethodSource 注释返回 Map

我在 Junit 测试用例上使用 @MethodSource 注释,以便从另一个方法接收 Map<String, Object>。

似乎 @MethodSource 无法支持“Map”对象。

这是我收到的错误:org.junit.platform.commons.PreconditionViolationException:无法将 java.util.HashMap 的实例转换为流:{1=Obj1, 2=Obj2}

您知道是否有一种方法可以像本例中那样接收“Map”对象?

@ParameterizedTest
@MethodSource("hashMapProvider")
void testMyMapObj(Map<String, Object> argument) {
    assertNotNull(argument);
    Object obj1 = argument.get("1");
}


static Map<String, Object> hashMapProvider() {
    Map<String, Object> map = new HashMap<>();
    map.put("1", "Obj1");
    map.put("2", "Obj2");
    return map;
 }
Run Code Online (Sandbox Code Playgroud)

java junit5

4
推荐指数
1
解决办法
3541
查看次数

我可以获得 Spring Boot Web 应用程序中的所有验证错误吗?

我有一个如下所示的 pojo(请假设一个控制器和其余代码。应用程序位于 Spring boot 中):

@Getter  @Setter
@AllArgsConstructor  @NoArgsConstructor
public class User  {
    @NotBlank(message = "userName is blank")
    private String userName;

    @NotBlank(message = "secretKey is blank")
    private String secretKey;
}
Run Code Online (Sandbox Code Playgroud)

并定义了一个 ExceptionHandler 类,@ControllerAdvice并定义了一个方法,如下所示:

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    protected ResponseEntity<ErrorResponse> handleMethodArgNotValidException(MethodArgumentNotValidException ex,Locale locale) {
        // code to handle exception.
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = {WebExchangeBindException.class})
    protected ResponseEntity<ErrorResponse> handleException(WebExchangeBindException ex, Locale locale) {
        // code to handle exception.
    }
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,即使两个字段都有验证错误,客户端也只能得到一个。

我想问有什么方法可以列出该端点响应中的所有验证错误吗?

curl --location --request POST 'localhost/api/login' \
--header 'Content-Type: application/json' \ …
Run Code Online (Sandbox Code Playgroud)

java validation exceptionhandler spring-boot

2
推荐指数
1
解决办法
3388
查看次数