Tag: 春季

如何注入接口的具体实现?

在spring(启动)我有一个接口的多个实现。 当注入接口时,我想定义我要注入哪个特定的实现。 我怎样才能做到这一点?

jackson自定义序列化程序或值对象?

几年后,我重新回到了后端开发。 我选择了Kotlin的Spring Boot。 我想有一个以域为中心的,一般的,可重用的模型对象。 对于服务调用,我希望返回特定于用例的响应,其中,有效负载进出只是特定场景的信息。 (我们可以称之为“合同优先”或“不要泄露你的胆量”) 在过去,我可能已经通过使用像Dozer这样的框架将来自可重用模型对象的信息映射到可序列化的值对象上来实现服务。 或者,我正在考虑为给定的端点注册一个自定义序列化器,例如Django Rest Framework。 (不知道这是如何工作jackson序列化通过注释)。 问题: 有没有一种简单的方法来使用jackson自定义序列化器来做我想要的? 还是继续使用我多年前使用的价值对象方法会更好?

Spring Security多个成功的认证提供商

我希望我的Web应用程序的用户通过LDAP和其他自定义身份validation进行身份validation。 这是一个用Kotlin编写的Spring Boot应用程序。 我已经配置AuthenticationManagerBuilder如下 @Autowired lateinit var authenticationProvider: CustomAuthenticationProvider override fun configure(auth: AuthenticationManagerBuilder) { auth .authenticationProvider(authenticationProvider) auth .ldapAuthentication() .userDnPatterns(“uid={0},ou=people”) .groupSearchBase(“ou=groups”) .contextSource() .url(“ldap://localhost:8389/dc=example,dc=com”) .and() .passwordCompare() .passwordEncoder(PlaintextPasswordEncoder()) .passwordAttribute(“userPassword”) } 我想连锁身份validation,以便如果CustomAuthenticationProvider成功身份validation(function身份validation不会抛出)身份validation继续使用LDAP身份validation提供程序。 如果CustomAuthenticationProvider成功进行身份validation,则写入LDAP身份validation(以及任何后续的身份validation提供程序)不会被评估。 只有在CustomAuthenticationProvider引发时才执行LDAP认证。 我已经阅读了许多文章(例如Spring Security中的多个身份validation提供者),详细描述了具有多个身份validation提供程序但具有OR行为而非AND行为的文章。 有什么建议么?

如何用Spring WebFlux返回404

我有一个这样的控制器(在Kotlin中): @RestController @RequestMapping(“/”) class CustomerController (private val service: CustomerService) { @GetMapping(“/{id}”) fun findById(@PathVariable id: String, @RequestHeader(value = IF_NONE_MATCH) versionHeader: String?): Mono<HttpEntity> = return service.findById(id) .switchIfEmpty(Mono.error(NotFoundException())) .map { // ETag stuff … ok().eTag(“…”).body(…) } } 我想知道是否有一个更好的方法比抛出一个exception是用@ResponseStatus(code = NOT_FOUND)注解@ResponseStatus(code = NOT_FOUND)

在Spring Boot 2中解开Thymeleaf中的Kotlin HashMap

我有一个Kotlin函数,它创建一个带有hashmap的模型,如下所示 @GetMapping(“/”) fun index(model: Model): Mono { model.addAttribute(“images”, imageService.findAllImages()?.flatMap { image -> Mono.just(image) .zipWith(repository.findByImageId(image?.id!!).collectList()) .map({ imageAndComments: Tuple2<Image?, MutableList> -> hashMapOf( “id” to imageAndComments.t1?.id, “name” to imageAndComments.t1?.name, “comments” to imageAndComments.t2) }).log(“findAllImages”) }) model.addAttribute(“extra”, “DevTools can also detech code changes.”) return Mono.just(“index”) } Image.kt package learningspringboot.images import org.springframework.data.annotation.Id data class Image(@Id var id: String? = null, var […]

在Kotlin中创建来自Spring配置的列表的映射

我正在尝试在Kotlin编写的Spring Boot应用程序中创建Map<String, List>types的对象。 我能够从配置创建一个映射,也能够从配置创建一个列表,但是当我尝试并结合这两个我得到以下exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘myConfiguration’: Could not bind properties to MyConfiguration (prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException 我的Configuration Object : @ConfigurationProperties @Component class MyConfiguration { var myNewMap: Map<String, List>? = null } 我的Configuration yml : — myNewMap: firstKey: – 2 – 4 secondKey: – 2 这是可能的方式在阅读配置的Spring? 或者是唯一的方法来创建一个简单的地图的值作为一个逗号分隔的字符串,并把它变成我的应用程序中的列表? […]

如何将@Value属性注入到使用Spring 5和Kotlin Bean Definition DSL定义的bean中

我正在开发一个使用Kotlin的Spring Boot(2.0.0 M7)应用程序,并且需要定义一些bean来利用新的DSL来定义bean。 我不能想出一个方法来注入来自@Value属性的值。 我们来考虑这个简化的例子: fun beans() = beans { for (i in 1..10) { bean(“myString${i}” + someProperty) { “myString${i}” + someProperty} } } someProperty应该来自这样的东西: @Value(“\${myProperty}”) someProperty: String 我如何使它可以访问beans {} DSL?

IntelliJ Idea 2017.3无法启动Kotlin Spring Boot App – @Configuration类可能不是最终的

我能够从IntelliJ 2017.3推出一个春季启动Kotlin应用程序。 在最后一次IntelliJ修复更新后,我无法从IDE启动该应用程序,得到此exception: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class ‘AccessConfig’ may not be final 我可以像往常一样从终端启动它: java -jar xxxx.jar 这是没有任何意义的,因为我在我的Gradle配置中使用必要的Kotlin Spring插件: buildscript { ext { kotlinVersion = ‘1.2.21’ springBootVersion = ‘2.0.0.RC1’ } repositories { mavenCentral() maven { url “https://repo.spring.io/snapshot” } maven { url “https://repo.spring.io/milestone” } jcenter() maven { url “https://plugins.gradle.org/m2/” } } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}”) classpath(“org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}”) classpath(“org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}”) […]

使用命令行参数的Kotlin Gradle Spring bootRun任务

我有一个像下面的命令行亚军Kotlin春季启动应用程序。 @SpringBootApplication class MySpringApplication : CommandLineRunner { override fun run(vararg args: String?) { println(args[0]) } } fun main(args: Array) { val app = SpringApplication(MySpringApplication::class.java) app.setBannerMode(Banner.Mode.OFF) app.run(*args) } 我想知道如何通过命令行参数,当我使用gradle命令,./ ./gradlew bootRun ?

Spring引导validation注释不适用于Kotlin

我为Kotlin获得了一个简单的数据类 data class Person(val name: String, @get: Min(18) val age: Int) 我实际上是从CSV文件构建这个类,并使用apache CSV解析器读取CSV。 即使我有一些age小于18的数据,测试仍然没有通过任何错误。 看起来这个注释不适用于Kotlin?