小编ams*_*ger的帖子

是否可以使 Spring Boot 配置处理器与值是复杂结构的映射正常工作(在 Intellij IDEA 中)?

重现问题的源代码:链接

假设我有这种配置属性的结构:

    @Data
    @ConfigurationProperties(prefix = "props")
    public class ConfigProperties {
    
        private String testString;
        private Map<String, InnerConfigProperties> testMap;
    }
Run Code Online (Sandbox Code Playgroud)
    @Data
    public class InnerConfigProperties {
    
        private String innerString;
        private Integer innerInt;
    }
Run Code Online (Sandbox Code Playgroud)

application.yml我这样设置它们:

props:
  testString: asdadasd
  somWrongProperty: asdasd
  testMap:
    key1:
      innerString: value1
      innerInt: 1
      someInnerWrongProperty: wrongvalue
    key2:
      innerString: value2
      innerInt: 2
Run Code Online (Sandbox Code Playgroud)

启动注释处理后,只有简单的属性可以正常工作(您可以通过单击 导航到它们的声明ctrl,它们的自动完成也可以工作)。此外,IDEA 还会检测该属性是否不正确并突出显示它。

对于嵌套结构(即映射值),这两个功能似乎都无法正常工作。您仍然可以单击它们,但 IDEA 将导航到地图声明。此外,地图值的代码完成和错误字段的突出显示不起作用。

IDEA 截图:

在此输入图像描述

有谁知道如何使其正常工作?请随意使用随附的示例代码。

提前致谢。

更新

似乎已在 Intellij IDEA 2022.1 中修复。相关问题:IDEA-151708IDEA-159276

不过,修复错误的效率不错。

java spring intellij-idea spring-boot configurationproperties

7
推荐指数
1
解决办法
1812
查看次数

在春季模拟RestTemplate

我正在尝试测试我的春季休假客户,但遇到了麻烦。这是我的客户课程:

@Component
public class MyClient{    

    private RestTemplate restTemplate;


    @Autowired
    public MyClient(RestTemplateBuilder restTemplateBuilder,ResponseErrorHandler myResponseErrorHandler) {
        this.restTemplate = restTemplateBuilder
                .errorHandler(myResponseErrorHandler)
                .build();
    }
    //other codes here
}
Run Code Online (Sandbox Code Playgroud)

myResponseErrorHandler 是重写类的类 handleErrorhasError方法ResponseErrorHandler

现在我的测试课是

    @RunWith(SpringRunner.class)
    public class MyClientTest {    

        @InjectMocks
        MyClient myClient;
        @Mock
        RestTemplate restTemplate;
        @Mock
        RestTemplateBuilder restTemplateBuilder;

       //test cases here
    }
Run Code Online (Sandbox Code Playgroud)

但是出现以下错误,并且不确定如何解决。

You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null
Run Code Online (Sandbox Code Playgroud)

junit mockito resttemplate

6
推荐指数
2
解决办法
636
查看次数

当我尝试在Spring控制器的方法中将json字符串映射到java域类时出现MethodArgumentConversionNotSupportedException

从前端我收到GET-request,其中包含已编码的json字符串作为其参数之一:

http://localhost:8080/engine/template/get-templates?context=%7B%22entityType%22%3A%22DOCUMENT%22%2C%22entityId%22%3A%22c7a2a0c6-fd34-4f33-9cb8-14c2090565ea%22%7D&page=1&start=0&limit=25  
Run Code Online (Sandbox Code Playgroud)

没有编码的Json参数“上下文”(UUID是随机的):

{"entityType":"DOCUMENT","entityId":"c7a2a0c6-fd34-4f33-9cb8-14c2090565ea"}
Run Code Online (Sandbox Code Playgroud)

在后端,处理该请求的控制器方法如下所示:

@RequestMapping(value = "/get-templates", method = RequestMethod.GET)
public List<Template> getTemplates(@RequestParam(required = false, name = "context") Context context) {
   //...
}
Run Code Online (Sandbox Code Playgroud)

“上下文”域类:

public class Context {
    private String entityType;
    private UUID entityId;

    public String getEntityType() {
        return entityType;
    }
    public void setEntityType(String entityType) {
        this.entityType = entityType;
    }
    public UUID getEntityId() {
        return entityId;
    }
    public void setEntityId(UUID entityId) {
        this.entityId = entityId;
    }
}
Run Code Online (Sandbox Code Playgroud)

我相信Spring的Jackson模块会自动将这种json转换为Context类的java对象,但是当我运行此代码时,它给了我异常:

org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type …
Run Code Online (Sandbox Code Playgroud)

java spring jackson

5
推荐指数
1
解决办法
3275
查看次数

如何使用 Jackson ObjectMapper 反序列化 Spring 的 ResponseEntity (可能使用 @JsonCreator 和 Jackson mixins)

该类ResponseEntity 没有默认构造函数。然后,为了使用 objectMapper 反序列化它,我决定使用 araqnid 在该答案中给出的方法。很快 - 它需要使用 Jackson 的 mixin 功能和 @JsonCreator。

就我而言(使用 ResponseEntity),由于不同的原因,它还没有解决。

我的测试方法如下所示:

public static void main(String[] args) throws Exception {
    ResponseEntity<Object> build = ResponseEntity.ok().build();

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.addMixIn(ResponseEntity.class, ResponseEntityMixin.class);

    String s = objectMapper.writeValueAsString(build);
    ResponseEntity<Object> result = objectMapper.readValue(s, ResponseEntity.class);

    System.out.println(result);
}
Run Code Online (Sandbox Code Playgroud)

首先,我尝试使用最短的 mixin 构造函数:

public abstract static class ResponseEntityMixin {
    @JsonCreator
    public ResponseEntityMixin(@JsonProperty("status") HttpStatus status) {
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我收到一个断言错误,因为ResponseEntity它的构造函数中有这行代码:

Assert.notNull(status, "HttpStatus must not be null");
Run Code Online (Sandbox Code Playgroud)

然后我将@JsonCreator的模式切换为,DELEGATING但在这种情况下我得到了另一个异常:

Exception …
Run Code Online (Sandbox Code Playgroud)

java spring mixins jackson deserialization

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

在 Spring Data JPA 中,派生的 find 方法在一个事务中多次调用时不使用一级缓存,但默认的 findById 会使用一级缓存

我在存储库中创建了一个自定义(派生)查找方法:

Optional<User> findUserById(Long id);
Run Code Online (Sandbox Code Playgroud)

findById(...)它的签名与继承自的默认签名 ( ) 相同,JpaRepository但它们的行为有点不同:

  • 如果派生方法在方法内部使用 多次调用,则派生方法似乎不尊重一级缓存(持久上下文)@Transactional,因此,我们会得到多个 select 语句。
  • 另一方面,默认findById方法使用一级缓存,并且在与上述相同的条件下,实际上只执行一条 select 语句(接下来的调用获取缓存的结果)。

即使我的自定义方法也被调用,也会发生相同的(多个选择)findById(但在这种情况下,我们显然不能使用默认方法,因为它被它遮蔽了)。

有谁知道为什么会发生这种情况?提前致谢。

我正在使用 Spring Boot 2.3.4.RELEASE / Hibernate 5.4.21.Final

下面是一些有意义的代码(带有测试的完整项目可以在Github上找到):

用户实体

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Id
    private Long id;
    private String name;
}
Run Code Online (Sandbox Code Playgroud)

用户存储库

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    Optional<User> findUserById(Long id);
}
Run Code Online (Sandbox Code Playgroud)

数据库服务

@Service
@RequiredArgsConstructor
@Slf4j
public class DatabaseService {

    private final UserRepository userRepository; …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-data-jpa spring-boot

3
推荐指数
1
解决办法
1486
查看次数

Spring boot 中的外部化配置

我有一个外部配置文件(外部 jar)。我尝试运行并期望外部文件中的值将覆盖内部文件中的值(application.properties\resource\- jar 文件中)。我阅读文档并尝试这个:

java -jar ccgame-1.0.jar --spring.config.location=classpath:/application.properties,file:/production.properties
Run Code Online (Sandbox Code Playgroud)

这不起作用。

我的 jar 文件位于\target\目录和我的production.properties太(at \target\)

我该如何解决我的问题?

  • 我应该把外部配置文件放在哪里?
  • 而我必须做什么?

java spring spring-boot spring-boot-configuration

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