Tag: 春季启动

如何在Spring WebFlux中记录请求和响应主体

我想用Kotlin在Spring WebFlux的REST API中集中记录请求和响应。 到目前为止我已经尝试过这种方法 @Bean fun apiRouter() = router { (accept(MediaType.APPLICATION_JSON) and “/api”).nest { “/user”.nest { GET(“/”, userHandler::listUsers) POST(“/{userId}”, userHandler::updateUser) } } }.filter { request, next -> logger.info { “Processing request $request with body ${request.bodyToMono()}” } next.handle(request).doOnSuccess { logger.info { “Handling with response $it” } } } 这里的请求方法和路径日志成功,但身体是Mono ,所以我应该如何登录? 应该是相反的方式,我必须订阅请求正文Mono ,并在回调登录? 另一个问题是ServerResponse接口在这里不能访问响应主体。 我怎样才能在这里? 我试过的另一种方法是使用WebFilter @Bean […]

在springMVC 3,如何写一个cookie,同时返回一个ModelAndView?

我的控制器方法返回一个ModelAndView ,但也有一个要求写一个cookie到客户端。 在spring可以做到吗? 谢谢。

无法通过Springboot连接到远程MongoDB

我试图连接到一个远程的MongoDB。 由于我没有得到一个MongoOpenSocketException,我认为已经建立了一个连接,但需要更多的启动应用程序。 我错过了什么? Application.kt package hello import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration import org.springframework.context.annotation.Configuration /** * The main entry point to the application */ @EnableAutoConfiguration(exclude = arrayOf(MongoAutoConfiguration::class)) @Configuration class Application /** * Run the application * @param args The command line arguments */ fun main(args: Array) { SpringApplication.run(Application::class.java, *args) } 的build.gradle: // Top-level build file […]

Kotlin Spring Boot使用Map创建urlencoded POST请求

我刚开始玩Kotlin和Spring Boot,并决定编写一个简单的端点,它需要一个表单urlencode的POST请求。 我不想为身体写一个实际的数据类,所以我试图只使用一个地图为身体,希望我可以访问键/值对。 我第一次尝试: @RestController class MyController { @RequestMapping(value = “/endpoint”, method = arrayOf(RequestMethod.POST), consumes = arrayOf(“application/x-www-form-urlencoded”)) fun myEndpoint(@RequestBody body: Map): String { // Do stuff } } 但是,这导致了关于不支持媒体types的415错误…我读的是由于使用@RequestBody和表单urlencoded POST。 我随后尝试使用@ModelAttribute,而不是接收 无法实例化[java.util.Map]:指定的类是一个接口 我完全是黑客攻击,这并不奇怪。 我也尝试没有任何身体的注释,但没有注入任何forms的参数。 我知道我可以添加一个数据类来解决这个问题,但我想知道这是否可以一般地使用Map来完成,因为我之前在Java中做过类似的事情。 谢谢。

如何配置使用klaxon库的spring引导

有一个klaxon库 – 用于kotlin JSON解析器 如何配置Spring Boot来使用它来创建一个REST API: @RestController class SampleController { @RequestMapping(“/test”, method = [RequestMethod.POST]) fun test(@RequestBody body:JsonObject): JsonObject { //work with body val (KLAXON object) //return KLAXON object } } @RequestBody body:JsonObject – 是一个Klaxon对象,所以我们不想使用标准的Jackson2ObjectMapperBuilder for RequestBody。 为了简单起见,我们不希望将它用于Response主体。 Post body是一些动态数据,所以我想在lib中使用Low level API ,而不是Object binding API 。

Spring Boot无法在IntelliJ中运行单个测试

最近开始发生,但我不确定是什么改变了。 当我从IntelliJ运行所有测试时,一切都很好。 而且gradle build也很好。 当我运行一个unit testing时,一切都很好。 当我运行一个Web集成测试时,它失败,因为一个配置类具有所有的空属性。 配置类看起来像(Kotlin): @Component @ConfigurationProperties(prefix = “api”) public open class ApiConfigImpl : ApiConfig { 一个测试看起来像: @RunWith(SpringJUnit4ClassRunner::class) @ContextConfiguration(classes = arrayOf(ApplicationAssembly::class), loader = SpringApplicationContextLoader::class) @WebIntegrationTest open class CandidateProfileControllerTest { @Inject lateinit var profileRepo: CandidateProfileRepository //etc a few more deps used to setup test data @Test open fun getById() { val greg = […]

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

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

未解决的参考:使用@ExtendWith注释的SpringExtension

我正在使用SpringBoot中的Junit 5来利用它提供的一些新function。 基本的设置工作,我可以使用新的注释,如@BeforeAll和@AfterAll但是,我相信,因为我不能解决@ExtendWith(SpringExtension::class)我不能初始化我的测试类,以使用注入资源。 我已经尝试将所需的依赖项添加到我的build.gradle文件,但我假设我缺少一个键依赖项: buildscript { ext.dokka_version = ‘0.9.15’ ext.dockerVersion = ‘1.2’ ext.junit4Version = ‘4.12’ ext.junitVintageVersion = ‘4.12.2’ ext.junitPlatformVersion = ‘1.0.2’ ext.junitJupiterVersion = ‘5.0.2’ ext.kotlin_version = ‘1.1.60’ ext.log4jVersion = ‘2.9.0’ ext.springVersion = ‘1.5.8.RELEASE’ repositories { mavenCentral() jcenter() } dependencies { // Needed for the ‘kotlin’ plugin classpath(“org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}”) // Needed for the ‘org.springframework.boot’ plugin classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springVersion}”) // […]

在intellij中运行并执行jar时的微妙差异

我正在用尽想法 当我运行在indellij和gradle理念插件应用 ( 应用插件:’想法’ )时,我的春季启动应用程序表现良好。 一旦我从build.gradle中移除插件,它的行为与使用java -jar app.jar执行的应用程序的行为类似 – 下面的描述存在细微但重要的区别。 我有以下情况,由于某些exception,当前tx失败,tx被标记为仅回滚,exception被捕获,并且其处理包括使用TransactionSynchronizationManager.registerSynchronization (new tx)注册后续tx恢复活动。 代码工作正常与思想插件intellij,当我删除插件声明或运行与java -jar注册过程(后tx失败任务)的弹簧启动jar失败,例外: Caused by: java.lang.IllegalStateException: Transaction synchronization is not active at org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(TransactionSynchronizationManager.java:291) ~[spring-tx-4.3.10.RELEASE.jar!/:4.3.10.RELEASE] 顺便说一句,代码是在kotlin,如果它很重要。 有任何想法吗? UPDATE 我觉得有一种竞争条件,因为在调试模式,即使没有理念的插件,应用程序的行为如预期(注册过程成功)。

Spring WebFlux:只有一个连接接收用户允许

我正在用Spring 5 Webflux和Kotlin编写一个简单的应用程序。 我正在尝试以下列方式实现PUT端点: PUT(“/confs/{id}”, { val id = it.pathVariable(“id”) ServerResponse.ok().body(service.save(it.bodyToMono(Item::class.java)), Item::class.java) }) 保存技巧是我尝试从项目中读取城市名称,解析地理坐标,在原始项目中覆盖它们,然后使用Spring Data Mongo Reactive回购保存到Mongo。 fun save(item: Mono): Mono { val geo = item.flatMap { val city = it.location?.city ?: “Somewhere” geoService.resolveGeoFromCity(city) } val zipped = item.zipWith(geo) .map { it.t1.location?.geo = it.t2 it.t1 } return repo.saveAll(zipped) .toMono() } 解析地理坐标的代码在这里: @Service class GeoService() { […]