Tag: 弹簧

弹簧5反应堆 – 每1秒发射一次物品

我试图每秒发出一次价值 Flux.just(User("A"), User("B"), User("C")).delayElements(Duration.ofSeconds(1)) 但是它以1秒的启动延迟立刻发出一切。 我怎样才能引入每个元素发射的延迟?

如何使用onErrorMap()处理异常时如何访问Mono <T>?

在我定义的数据类中, 'name'在整个mongo集合中必须是唯一的: @Document data class Inn(@Indexed(unique = true) val name: String, val description: String) { @Id var id: String = UUID.randomUUID().toString() var intro: String = "" } 所以在服务中,如果有人再次传递相同的名字,我必须捕获意外的异常。 @Service class InnService(val repository: InnRepository) { fun create(inn: Mono<Inn>): Mono<Inn> = repository .create(inn) .onErrorMap( DuplicateKeyException::class.java, { err -> InnAlreadyExistedException("The inn already existed", err) } ) } 这是好的,但如果我想添加更多的信息,如"The […]

Spring Boot和Kotlin中的部分更新REST

我有一个Spring Boot + Kotlin + Morphia项目。 我需要添加我的实体的部分更新。 我的实际发布方法: @PostMapping("update/") fun updateStudent(@RequestBody @Valid student: Student, results: BindingResult): ResponseData<Student> { if (results.hasErrors()) return ResponseData(errors = results.errors) if (!student.canEdit(login.user)) return ResponseData() student.save() return ResponseData(data = student) } 我需要从数据库中读取学生,只更新发回的字段

Spring @PostConstruct取决于@Profile

我想在一个配置类中有多个@PostConstruct注释的方法,这应该被称为依赖于@Profile。 你可以想象一个代码如下所示: @Configuration public class SilentaConfiguration { private static final Logger LOG = LoggerFactory.getLogger(SilentaConfiguration.class); @Autowired private Environment env; @PostConstruct @Profile("test") public void logImportantInfomationForTest() { LOG.info("********** logImportantInfomationForTest"); } @PostConstruct @Profile("development") public void logImportantInfomationForDevelopment() { LOG.info("********** logImportantInfomationForDevelopment"); } } 但是根据@PostConstruct的javadoc,我只能使用这个注释来注释一个方法。 在Spring的Jira https://jira.spring.io/browse/SPR-12433中有一个改进。 你如何解决这个要求? 我总是可以将这个配置类分成多个类,但也许你有一个更好的主意/解决方案。 BTW。 上面的代码运行没有问题,但是无论配置文件设置如何,都会调用这两个方法。

测试Spring的@Async void返回方法

我在我的Spring应用程序中返回void (或Unit ,我正在写Kotlin)的@Async方法有点问题。 我不知道为什么,但是当@Async方法返回void它只是不执行,或者至少它不会做它应该的。 需要说的是,在我的异步方法中,我想用JavaMailSender发送一封邮件,所以没什么大不了的。 这里是方法: @Async override fun sendEmail(locale: Locale, subject: String, message: String, to: String) { val msg = sender.createMimeMessage() println("1") val helper = MimeMessageHelper(msg, true, "utf-8") helper.setFrom("email@example.com") helper.setTo(to) println("2") helper.setSubject(getSubject(locale, null)) println("3") helper.setText(processTemplate(locale, null, null), true) println("4") sender.send(msg) println("5") } 但电子邮件永远不会来,没有异常记录(我正在运行testNG测试)。 当我改变函数的签名使其返回Future<String>并在函数的末尾添加一些虚拟的返回行,然后调用service.sendEmail(…).get() ,方法的主体以奇迹般的方式执行和电子邮件到达。 在我的@Configuration类中,有@EnableAsync 。 我还实现了AsyncConfigurer并提供了自己的执行程序和异常处理程序,因为我认为它可能是我的执行程序bean定义的东西,但没有任何帮助。 这让我疯狂,因为我只是想在后台默默地执行一些东西,而且不起作用。 我默默地说,我不想被内部抛出的异常所困扰。 你有什么想法吗? 更新:所以建议@pleft,我在我的方法中打印一些打印。 现在,当我运行mvn clean […]

如何在kotlin中使用@Autowired这样的Spring注解?

是否有可能在Kotlin做下面的事情? @Autowired internal var mongoTemplate: MongoTemplate @Autowired internal var solrClient: SolrClient

带有Jetty + Jersey的Spring Boot始终显示404

我有一个奇怪的问题。 我使用Spring boot 1.3.3和mvc(不包括tomcat),jetty和jersey(JAX-RS注释)开发项目。 我的端点完美地运行从IDEA运行组装的java – 罐子.JAR。 但后来我试图使用spring-boot-maven-plugin中的可执行文件选项来制作我的JAR可执行文件。 我的项目仍然做出正确的请求,如果我从IDEA启动它,但编译的可执行文件.JAR总是发送404错误。 好的,我在JerseyConfig类中做了一些修改。 我替换了 packages(ru.finnetrolle.telebot.restful.JerseyConfig::class.java.`package`.name) 同 register(BroadcastResource::class.java) 和可执行JAR在我的开发人员的PC上正常工作。 但是,当我尝试在我的Linux服务器上组装它,并将simlink添加到init.d – 所有端点再次开始对所有请求说404。 如果我从本地和从远程主机请求,我有404。 我也补充一点 spring.jersey.application-path=/ server.servlet-path=/monitoring 到我的application.properties文件(我也使用Spring执行器),但没有任何变化 – 所有工作没有和不工作 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> 我也发现了一个问题描述我的问题,但没有帮助https://github.com/spring-projects/spring-boot/issues/3413 PS我正在使用Kotlin,但是这不应该影响这个

Kotlin NoClassDefFoundError和spring @RequestMapping方法

我试图用Kotlin + IntelliJ来学习一些春天的基础知识,而且我遇到了一些麻烦。 @RequestMapping带注释的方法(带参数)会导致异常,而Java等价物完全正常。 Kotlin代码工作正常,但只有没有“processFormTwo”方法(将工作Java代码转换为kotlin也无济于事)。 这里是代码: @Controller class HelloWorldController { @RequestMapping("/showForm") fun showForm() = "helloworld-form" // this one works fine @RequestMapping("/processForm") fun processForm() = "helloworld" //this one doesn't @RequestMapping("/processFormTwo") fun processFormTwo(request: HttpServletRequest, model: Model): String { var theName = request.getParameter("studentName") theName = theName.toUpperCase() val result = "Yo! " + theName model.addAttribute("message", result) return "helloworld" } […]

用Spring 5 WebFlux框架解码ByteArray

我试图用kotlin来使用新的Spring WebFlux框架。 而我无法找到这个代码(myService)的错误: fun foo(): Flux<ByteArray> { val client = WebClient.create("http://byte-array-service") return client .get() .uri("/info") .accept(MediaType.APPLICATION_OCTET_STREAM) .exchange() .flatMapMany { r -> r.bodyToFlux(ByteArray::class.java) } } 这个方法返回Flux 7893字节,我知道并不是byte-array-service发送的所有字节。 如果我使用旧式的休息模板一切正常 fun foo(): Flux<ByteArray> { val rt = RestTemplate() rt.messageConverters.add( ByteArrayHttpMessageConverter()) val headers = HttpHeaders() headers.accept = listOf(MediaType.APPLICATION_OCTET_STREAM) val entity = HttpEntity<String>(headers) val r = rt.exchange("http://byte-array-service/info", HttpMethod.GET,entity, ByteArray::class.java) return […]

如何检查单声道是否为空?

我正在使用WebFlux框架开发Spring Boot 2.0和Kotlin的应用程序。 我想检查一个用户ID是否保存一个事务之前退出。 我被困在一个简单的东西,如验证单声道是空的。 fun createTransaction(serverRequest: ServerRequest) : Mono<ServerResponse> { val transaction = serverRequest.body(BodyExtractors.toMono(Transaction::class.java)) transaction.flatMap { val user = userRepository.findById(it.userId) // If it's empty, return badRequest() } return transaction.flatMap { transactionRepository.save(it).then(created(URI.create("/transaction/" + it.id)).build()) } } 有可能做我想做的事情?