杰克逊数据绑定类路径问题(Jackson Databind classpath issue)
我有一个弹簧启动应用程序,当我在我的本地使用“mvn clean install”进行部署时工作正常,但是当通过Jenkin生成战争时,它会抛出以下错误。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapper' defined in class path resource [com/test/common/TestRestConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.SerializationConfig at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ... 62 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.SerializationConfig at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ... 74 common frames omitted Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.SerializationConfig at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:535) at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:452)
我只是尝试比较使用无法比较的2个战争文件,除了用于编译的JDK次要版本之外,我没有看到任何差异。
我试图在我的本地版本和jenkin版本中搜索SerializationConfig.class,
下面的命令的输出是,
find . -type f -name '*.jar' -print0 | xargs -0 -I '{}' sh -c 'jar tf {} | grep SerializationConfig.class && echo {}'
局部战争O / P: -
com/fasterxml/jackson/databind/SerializationConfig.class ./jackson-databind-2.7.3.jar org/codehaus/jackson/map/SerializationConfig.class ./jackson-mapper-asl-1.9.13.jar com/fasterxml/jackson/databind/SerializationConfig.class ./jersey-all-2.18.jar
詹金战争O / P: -
com/fasterxml/jackson/databind/SerializationConfig.class ./jersey-all-2.18.jar org/codehaus/jackson/map/SerializationConfig.class ./jackson-mapper-asl-1.9.13.jar com/fasterxml/jackson/databind/SerializationConfig.class ./jackson-databind-2.7.3.jar
基本上,我在我的TestRestConfiguration类中注入了ObjectMapper,如下所示,
@Inject private ObjectMapper objectMapper;
不确定,为什么通过Jenkin生成的战争文件造成问题。
任何帮助,将不胜感激。
I have a spring boot app which works fine when i do the deploy using "mvn clean install" in my local, but when the war is generated through Jenkin, it is throwing the following error.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapper' defined in class path resource [com/test/common/TestRestConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.SerializationConfig at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ... 62 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.fasterxml.jackson.databind.ObjectMapper]: Factory method 'objectMapper' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.SerializationConfig at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ... 74 common frames omitted Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind.SerializationConfig at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:535) at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:452)
I just tried comparing the 2 war files using beyond compare and i dont see any diff except the JDK minor version being used to compile.
I tried to search for the SerializationConfig.class in my local build and in the jenkin build,
The output for the below command is,
find . -type f -name '*.jar' -print0 | xargs -0 -I '{}' sh -c 'jar tf {} | grep SerializationConfig.class && echo {}'
Local war O/P :-
com/fasterxml/jackson/databind/SerializationConfig.class ./jackson-databind-2.7.3.jar org/codehaus/jackson/map/SerializationConfig.class ./jackson-mapper-asl-1.9.13.jar com/fasterxml/jackson/databind/SerializationConfig.class ./jersey-all-2.18.jar
Jenkin war O/P :-
com/fasterxml/jackson/databind/SerializationConfig.class ./jersey-all-2.18.jar org/codehaus/jackson/map/SerializationConfig.class ./jackson-mapper-asl-1.9.13.jar com/fasterxml/jackson/databind/SerializationConfig.class ./jackson-databind-2.7.3.jar
Basically, I inject the ObjectMapper in my TestRestConfiguration class as follows,
@Inject private ObjectMapper objectMapper;
Not sure, why the war file being generated through Jenkin is causing problem.
Any help on this will be appreciated.
原文:https://stackoverflow.com/questions/39419160
最满意答案
通常,您希望使用初始化列表来初始化构造函数中的成员:
canvas() : fileName("zzz.txt") { }
当你这样做:
fileName = "zzz.txt";
您正在尝试为数组分配值,这是不合法的。 您可以使用值初始化数组,但之后不能为其分配值(作为整体)。 initializer-list语法允许您使用适当的值初始化它。
但是,如果您使用的是较旧的编译器,则可能必须使用
strcpy
:strcpy(fileName,"zzz.txt");
对于可以分配的其他类型,使用initializer-list语法并不是绝对必要的,但它通常仍然是一个好主意。
此外,正如其他人所指出的那样,除非你有充分的理由不这样做,否则你最好使用
std::string
而不是原始的字符数组。std::string
将允许任意长度的字符串,可以像常规值一样对待。In general, you want to use an initializer-list to initialize members in a constructor:
canvas() : fileName("zzz.txt") { }
When you do this:
fileName = "zzz.txt";
You are trying to assign a value to an array, which isn't legal. You can initialize an array with values, but you can't assign it a value (as a whole) afterwards. The initializer-list syntax allows you to initialize it with the proper value.
However, if you are using an older compiler, you may have to use
strcpy
instead:strcpy(fileName,"zzz.txt");
For other types that can be assigned, using the initializer-list syntax isn't strictly necessary, but it is still usually a good idea.
Also, as others have noted, unless you have a good reason not to, you'd be better off using a
std::string
instead of a raw array of characters. Astd::string
will allow for strings of arbitrary length which can be treated like regular values.
相关问答
更多-
通常,您希望使用初始化列表来初始化构造函数中的成员: canvas() : fileName("zzz.txt") { } 当你这样做: fileName = "zzz.txt"; 您正在尝试为数组分配值,这是不合法的。 您可以使用值初始化数组,但之后不能为其分配值(作为整体)。 initializer-list语法允许您使用适当的值初始化它。 但是,如果您使用的是较旧的编译器,则可能必须使用strcpy : strcpy(fileName,"zzz.txt"); 对于可以分配的其他类型,使用init ...
-
表达式必须是可修改的左值(当试图附加到数组时)(Expression must be a modifiable lvalue (when attempting to append to an array))[2023-10-30]
Hitcount += Br; 您无法为这样的C样式数组添加值。 您需要维护元素计数并添加如下值: Hitcount[count++] = Br; 或者你最好使用std::vector并通过调用push_back添加元素: std::vectorHitcount; // code skipped Hitcount.push_back( Br ); Hitcount += Br; You cannot add value to a C style array like that. ... -
将你的cin语句修改为cin >> a >> b; ,这会一次读取多个值,因为cin不存在逗号运算符。 另外,将赋值右侧的结果变量的顺序切换到左侧。 赋值中=的两个操作数是l值,代表左值,应该在左侧,而r值代表右值,应该在右侧。 切换它们,就会出错。 您的新修改代码 double a, b, result; cout << "Please enter your two side lengths (ex:12 5)"; cin >> a >> b; //<-- See '>>' here //sqrt( ...
-
C表达式必须是可修改的左值(为什么我要使用此表达式)(C expression must be a modifiable lvalue (Why am i getting on this expression))[2022-06-23]
您获得的错误与解析表达式的方式有关。 你的表达式(简化)如下所示: (a = b) < 10 ? c = 1 : c = 0 三元运算符?:具有比赋值运算符=更高的优先级。 虽然内部=被视为三元的一部分,但最右边的却不是。 所以表达式解析如下: ((a = b) < 10 ? c = 1 : c) = 0; 结果是您尝试将值0分配给不是左值的表达式,即变量名称或取消引用的指针。 你需要括号来解析你想要的方式: ((a = b) < 10) ? (c = 1) : (c = 0); 由于您正在做的是根据 ... -
您无法分配给阵列。 你可以做的是使用std::string或使用std::strcpy/std::strncpy ,如 std::strncpy(citizen1.name,"Tim McGuiness", sizeof(taxPayer::name)); 既然你使用C ++,我建议使用std::string , struct taxPayer { std::string name; // the rest }; 那么您可以像在代码中一样分配给它 citizen1.name = "Tim ...
-
c ++ IntelliSense:表达式必须是可修改的左值(c++ IntelliSense: expression must be a modifiable lvalue)[2022-05-07]
您的成员函数是const限定的。 除非声明为可变,否则不能在const限定的成员函数中修改任何成员变量。 你需要使pDrawMesh可变,从DrawMeshContainer删除const限定,或者找到一些其他方法来完成你想要完成的任何事情。 Your member function is const-qualified. You cannot modify any member variables from within a const-qualified member function unless t ... -
可修改的左值?(Modifiable lvalue?)[2023-04-15]
你的operator []返回一个int,它不是一个可修改的左值(大概是一个int不能放在赋值的左边)。 您的operator []也被定义得非常奇怪,为什么要编写递增的operator []? 这是你通常写这个的方式 int& operator [](const unsigned int n){return niz[n];} 现在你的代码应该工作,因为你正在返回一个可以递增的引用。 Your operator[] returns an int, which is not a modifiable lva ... -
表达式必须是可修改的结构数组上的可修改左值(expression must be a modifiable lvalue on a struct array that is modifiable)[2023-08-23]
数组不能直接在C中赋值。但是, 包含数组的结构是,所以你可以做的是: word w = { { {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1} } }; memory[0] = w; 使用C99,你可以使用复合文字并说: memory[0] = (struct word){ { {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1} } ... -
看来你的意思是以下 while (y != NULL && x == y->right) ^^^ 至于错误消息的含义,那么当你使用赋值运算符时,它的优先级小于等式和逻辑运算符的优先级,事实上 while ( ( y != NULL && x ) = y->right) 考虑到将y的声明放在else语句的代码块中要好得多,因为它仅在此范围内使用 //... else { node* y = x->parent; It seems that you me ...
-
这是因为您将一个整型常量传递给类似函数的宏FOO(a, b) ,它将预增加运算符应用于其参数。 此宏扩展的结果如下所示: int *p = graph_p + (--2)* 8 + (--3);; 这在C中是非法的,因为操作符需要一个可修改的左值作为其操作数。 这里的另一个问题是,你应该在宏的替代方案中放置一些关键字,并且不要在最后放置分号: #define FOO(a,b) ((--a)* 8 + (--b)) It's because you are passing an integer const ...