【发布时间】:2015-10-29 21:01:48
【问题描述】:
我遇到了 spring-boot、混合 java-scala web 应用程序的奇怪问题。 jar 被创建为org.springframework.boot.loader.PropertiesLauncher(layout=ZIP) jar,因此我可以使用 loader.path 加载外部库。我已经通过WebMvcConfigurerAdapter 使用spring-boot 注册了fastxml jackson-scala 模块中的DefaultScalaModule。我用 spring-boot-maven 插件创建了一个 uber jar。我使用 java -Dloader.path=xxx -jar myspringbootscala.jar 部署我的应用程序。它每次都在我的 mac os 上完美地部署和运行,但在 linux 上,我大部分时间都“随机”地出现以下错误。令人惊讶的是,它偶尔也会起作用!
错误 #1:
{
"timestamp": 1446079480082,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Instantiation of [simple type, class com.mycomp.DataRequest] value failed: null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class com.mycomp.DataRequest] value failed: null",
"path": "/myspringbootapp/v2/api/scdata"
}
如果我没有超过,那么我会得到以下结果:
错误 #2 这个并不像上面的那么可怕。这是一个 fastxml scala ojbectmapper 问题
29 Oct 2015 17:04:17.000 [http-nio-8280-exec-1] ERROR c.x.i.i.ControllerInterceptor- Exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:106)
at scala.Tuple2._1$mcI$sp(Tuple2.scala:19)
at com.mycomp.validator.DataRequestValidator$$anonfun$validate$1.apply(DataRequest\Validator.scala:94) //This line is simply boxing java.lang.String to scala.Int but runtime thinks it needs to convert java.lang.String to java.lang.Integer! I have `import scala.collection.JavaConversions._` on this class.
同样的东西,同样的 jar 在我的 mac 上也能完美地工作,具有相同的命令行选项和类路径。 scala 和 java 控制器(数据绑定)都可以正常工作。但是在 linux 上,只有 java 控制器(数据绑定)可以工作,而使用 scala,我在不同的时间会遇到上述错误之一。正如我所提到的,它有时也可以在 linux 上运行。
这很可能是一些外部因素造成的,但我无法弄清楚它是什么?
编辑: 我认为当我在 linux 上运行我的应用程序时,spring-boot 没有选择我设置的 scala 对象映射器,因此是 400 这是我设置对象映射器的方法
@Component
@EnableWebMvc
@Configuration
public class WebMvcCustomConfig extends WebMvcConfigurerAdapter {
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter scalaJsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper scalaObjectMapper = new ScalaObjectMapper();
//scalaObjectMapper.enable(features)
//objectMapper.registerModule(new DefaultScalaModule());
scalaObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
scalaJsonConverter.setObjectMapper(scalaObjectMapper);
return scalaJsonConverter;
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//converters.add(0, customJackson2HttpMessageConverter());
Iterator<HttpMessageConverter<?>> itr = converters.iterator();
while(itr.hasNext()){
HttpMessageConverter<?> conv = itr.next();
if(conv instanceof org.springframework.http.converter.json.MappingJackson2HttpMessageConverter){
itr.remove(); //my try-error attempt. doesnt work.
}
}
converters.add(0, customJackson2HttpMessageConverter()); //Replacing with my own customJackson2HttpMessageConverter
for(HttpMessageConverter<?> conv: converters){
System.out.println("httpconverter: "+ conv.getClass());
}
}
}
【问题讨论】:
-
所以你在同一个项目中混合了scalla控制器和java控制器,哪个部分在scala中,哪个在java中(我的意思是你的代码)?我有一个类似的项目,它工作正常,但一切都在 scala 中
-
我对 scala 和 java 有不同的父包层次结构。即 com.mycomp.scala 和 com.mycomp.java。 Java 和 Scala 控制器是分开的。
标签: java spring scala spring-boot