Tag: 春天

Spring Boot将文本/ JavaScript序列化为JSON

我创建了以下Kotlin数据类: @JsonInclude(JsonInclude.Include.NON_NULL) public data class ITunesArtist(val artistName: String, val artistId: Long, val artistLinkUrl: URL) (一个数据类是一个Kotlin类,在编译时自动生成equals,hashcode,toString等 – 节省时间)。 现在我试着使用Spring RestTemplate填充它: @Test fun loadArtist() { val restTemplate = RestTemplate() val artist = restTemplate.getForObject( "https://itunes.apple.com/search?term=howlin+wolf&entity=allArtist&limit=1", ITunesQueryResults::class.java); println("Got artist: $artist") } 它失败: Could not extract response: no suitable HttpMessageConverter found for response type [class vampr.api.service.authorization.facebook.ITunesArtist] and content type [text/javascript;charset=utf-8] […]

无法使用Gradle在IntelliJ Community Edition中添加应用程序服务器

我正在尝试使用spring mvc框架来构建Web服务。 我正在使用IntelliJ社区版IDE和gradle构建系统。 的build.gradle buildscript { ext { kotlinVersion = '1.1.3-2' springBootVersion = '1.5.6.RELEASE' } repositories { mavenCentral() } 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}") } } apply plugin: 'kotlin' apply plugin: 'kotlin-spring' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 compileKotlin { kotlinOptions.jvmTarget = "1.8" } compileTestKotlin { kotlinOptions.jvmTarget = "1.8" […]

Spring代理类和Kotlin中的空指针异常

我和春天一起面临着kotlin的一些问题。 我有一个控制器bean(没有接口btw),它有一个自动连线的服务bean通过主构造函数。 它工作完美,除非我使用控制器缓存注释。 显然弹簧缓存生成一个委托类下的缓存处理。 我的代码如下所示: @RestController @RequestMapping("/regions/") open class RegionController @Autowired constructor(val service: RegionService) { @RequestMapping("{id}", method = arrayOf(RequestMethod.GET)) @Cacheable(cacheNames = arrayOf("regions")) fun get(@PathVariable id: Long): RegionResource { return this.service.get(id) } } 现在的问题是执行该方法时出现空指针异常,实际上this.service为null ,在技术上是不可能的,因为它是kotlin中的非null变量。 我假设Spring生成的类代理使用空值而不是autowired bean初始化类。 这一定是使用kotlin和spring的常见错误。 你是怎么回避这个问题的?

使用Spring创建Scheduler Bean时发生NullPointerException

我想用Spring来创建一个调度器。 @Configuration @EnableScheduling public class MyScheduler { @Autowired MyBusinessService businessService; @Scheduled(cron = "* * * * * *") public void myCronMethod() { } } 在应用程序启动过程中,出现以下错误: java.lang.NullPointerException at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.resolveSchedulerBean(ScheduledAnnotationBeanPostProcessor.java:281) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:221) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:200) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:94) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337) at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068) at […]

如何禁用RepositoryRestHandlerMapping和EndpointHandlerMapping?

我目前正在使用Spring Boot,Hibernate和Spring-HATEOAS构建一个带有REST接口的应用程序。 我的数据模型被定义为具有@Entity批注的bean,我使用Spring的特性来自动设置一个Hibernate仓库(创建一个扩展PagingAndSortingRepository的界面)。 我的应用程序完全是注解驱动的,也就是说,我没有web.xml但是使用@Configuration @Bean , @Configuration @Bean等Spring注解来配置所有的东西,并且在SpringApplication.run(MyApp.class, args);的帮助下从我的main方法启动应用程序SpringApplication.run(MyApp.class, args); 这工作正常,但通过这种方法,创建了一个RepositoryRestHandlerMapping和EndpointHandlerMapping 。 这些创造了一堆我既不需要也不想要的资源。 我实现我自己的控制器,因为他们需要做比标准的逻辑更多。 我怎样才能防止这种默认行为,并禁用这些映射?

Spring使用OAuth令牌进行社交登录

道歉,我最近主要在iOS上工作,而且自从和Spring一起工作以来,我一直都是这么做的。 工具: 科特林 春季启动 春天的社交 问题: 我正在构建一个允许典型的“使用Facebook登录”类型功能的应用程序。 我想将iOS客户端获得的OAuth令牌发送到后端进行身份验证。 如果令牌有效,则后端将返回成功的认证,否则指示无效的凭证。 使用OAuth令牌登录后,将检索一些配置文件信息。 题: 大多数“入门指南”似乎都遵循使用用户名和密码凭证的OAuth身份验证。 使用Spring Social + Facebook,我该如何: 使用OAuth访问令牌进行身份验证并检索配置文件数据?

ReactiveCrudRespository永远不会从SaveAll返回

在启动时,我检查一些数据,如果不存在尝试保存一些默认值(暂时用于测试)。 val subs = repo.findAll().toIterable() if(subs.none()) { repo.saveAll(defaults.map { Source(it.link.hashCode().toLong(), it::class.java.canonicalName, arrayOf(it.link)) }).blockLast() } 在第一次运行时,我们将达到saveAll(),但从不解锁。 数据保存在MongoDB中,我可以用Robo 3t来确认。 随后运行的数据实际存在将导致第一次findAll从不解锁。 在MongoDB中的分析似乎显示出一个成功的查询。 findAll()查询的配置文件 我的资源库和实体如下: interface SourceRepository : ReactiveCrudRepository<Source, Long> { // } data class Source( @Id val id: Long, val type: String, val params: Array<String> ) 这是在Kotlin,反对Spring Boot 2.0.0.M4。 我的目标是在Docker中运行的MongoDB实例。 如果我删除了这一点的启动逻辑,我的另一个ReactiveCrudRepository能够读/写就好,永远不会阻塞。 工作仓库的saveAll()调用也在blockLast()中结束,因为我发现没有这个保存就不会实际发生。

以编程方式重新启动Spring Boot应用程序/刷新Spring上下文

我试图以编程方式重新启动我的Spring应用程序,而无需用户介入。 基本上,我有一个页面,允许切换应用程序的模式(实际上意味着切换当前活动的配置文件),据我所知我必须重新启动上下文。 目前我的代码非常简单,只是重启位(顺便说一下,这是Kotlin): context.close() application.setEnvironment(context.environment) ClassUtils.overrideThreadContextClassLoader(application.javaClass.classLoader) context = application.run(*argsArray) 然而,我做context.close()的时候立即存在JVM。 我也尝试了context.refresh()但似乎只是简单地杀死Tomcat / Jetty(尝试两个,以防万一它是一个Tomcat的问题),然后没有任何反应。 我也看到以编程方式重新启动Spring Boot应用程序,但从这些答案似乎没有任何工作。 此外,我看了一下据说具有/restart端点的弹簧执行器,但这似乎不再存在? 帮助将不胜感激。 谢谢。

Spring MVC控制器PathVariables上的Java bean验证

我试图让Java Bean验证注释在Spring MVC控制器中与路径变量和查询参数一起工作。 (环境:Spring Boot v1.3.5,Springxxx 4.2.6,Kotlin 1.0.3编程语言) 例如 @RequestMapping(value = "/{someId}" …) fun getSomething(**@SomeValidId** @PathVariable("someId") someId: String):… 我已经按照https://raymondhlee.wordpress.com/2015/08/29/validating-spring-mvc-request-mapping-method-parameters/中所述添加了org.springframework.validation.beanvalidation.MethodValidationPostProcessor,并且还添加了org。 springframework.validation.beanvalidation.LocalValidatorFactoryBean作为上面的validatorFactory。 @Configuration …class …. { … @Bean open fun localValidatorFactoryBean() = LocalValidatorFactoryBean() @Bean open fun methodValidationPostProcessor() : MethodValidationPostProcessor { val methodValidationPostProcessor = MethodValidationPostProcessor() methodValidationPostProcessor.setValidator(localValidatorFactoryBean()) return methodValidationPostProcessor } } 但是当我使用org.springframework.validation.annotation.Validated注解Controller类(或者它实现的接口)时,建议看起来像控制器类被代理(这看起来和预期的一样 – https://github.com/spring -projects / spring-security / issues […]

Spring在Kotlin中注入了参考

在Kotlin中,一个变量必须在声明时初始化,除非它有? 附加到类型名称。 所以Spring的注入bean必须被声明为: @AutoWired var someService: SomeService? = null 令人讨厌的是,从这里到处都有someService被使用,那么必须指定某种空安全逻辑,或者是直接的空检查。 当然我们可以这样做: @AutoWired var someService = new SomeService() 但是这并不总是可能的,一次性的实例只是混淆。 我的问题是,无论如何告诉Kotlin这个变量将被初始化,实际上不是null ?