The spring MVC project is integrated into the spring boot project

Posted by Patrick on Sat, 09 Oct 2021 14:26:13 +0200

Convert spring MVC project to spring boot project

Question 1: compatibility between mybatis and mybatis plus

Reference solution:

(12 messages) NoClassDefFoundError: org/mybatis/logging/LoggerFactory_ WeChat official account: backend code maker -CSDN blog

(12 messages) (pro test solution) Unsatisfied dependency expressed through bean property 'sqlSessionFactory'_ TianXinCoord's blog - CSDN blog

Two cannot exist in the pom.xml file at the same time, which may cause compatibility problems

Problem 2: ES configuration error

Error display: caused by: java.lang.noclassdeffounderror: org / elasticsearch / client / cancelable

Solution: add some content in the tag of pom.xml

        <java.version>1.8</java.version>
        <elasticsearch.version>7.6.2</elasticsearch.version>

Question 3: if the field value in Mysql obtained by mybaits plus is [] type, it is null

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-RGQXTFFm-1633783661627)(java problem summary. assets/image-20211002105109720.png)]

Because the attribute corresponding to the entity class defines TableField

As follows:

    /**
     * Role list
     */
    @TableField(typeHandler = JsonIntegerArrayTypeHandler.class)
    private Integer[] roleIds;

Therefore, you need to configure the scan handel in application.yml

#mybatis-plus
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.qf.pojo
  configuration:
    map-underscore-to-camel-case: true
  typeHandlersPackage: com.qf.handler   #To configure this scan

Explanation on the official website:

TypeHandler scanning path. If this property is configured, SqlSessionFactoryBean will register the class under the package as the corresponding TypeHandler; TypeHandler is usually used for custom type conversion.

Problem 4: spring boot integration mybatis plus paging failure

Plug in body (must see!) (since 3.4.0) | mybatis plus (baomidou. Com)

Add this configuration

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {

    /**
     * The new paging plug-in follows the rules of mybatis. MybatisConfiguration#useDeprecatedExecutor = false should be set to avoid cache problems (this attribute will be removed after the old plug-in is removed)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}

Question 5: add, delete and modify the association of multiple tables through the ID of Goods

When the primary key of an entity class is self incremented, the id (primary key) attribute of the entity class will be assigned to the automatically generated primary key after calling insert. If it is not obtained after insert, the id is empty, and the data inserted in all other associated tables are not associated with the commodity table

         dtsGoodsMapper.insert(goods);
        //Here, the id attribute in the commodity class after insert is obtained. Because it is self incremented, it can only be obtained after insert
        Integer gid = goods.getId();
        log.info("[Commodity management] commodity on the shelf->commodity ID´╝îResponse data:{}",gid);

        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(gid);
            dtsGoodsAttributeService.add(attribute);
        }

        for (DtsGoodsProduct product : products) {
            product.setGoodsId(gid);
            dtsGoodsProductService.add(product);
        }

        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(gid);
            dtsGoodsSpecificationService.add(specification);
        }
        log.info("[Commodity management] commodity modification->Products on the shelf, response results:{}","success");
        return ResponseUtil.ok();

Problem 6: jar package problem: no main list attribute

Question: shops_ There is no main manifest attribute in managenew.jar

Solution: add the following to the pom.xml file

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

When running mvn install, these main list attributes are automatically generated. When running java -jar xxx.jar, the startup class will be found according to the main list attributes to start the program.

Problem 7: JackSon problem during jar package startup

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-10-03 13:51:29.326 ERROR 39476 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'routerFunctionMapping' defined in class path resource [com/qf/config/SwaggerConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.function.support.RouterFunctionMapping]: Factory method 'routerFunctionMapping' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/TSFBuilder
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar!/:2.2.6.RELEASE]
        at com.qf.ApplicationWeb.main(ApplicationWeb.java:12) [classes!/:1.0-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_292]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_292]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_292]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_292]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [shops_manageNew-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [shops_manageNew-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51) [shops_manageNew-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52) [shops_manageNew-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.function.support.RouterFunctionMapping]: Factory method 'routerFunctionMapping' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/TSFBuilder
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        ... 27 common frames omitted
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/TSFBuilder
        at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder$SmileFactoryInitializer.create(Jackson2ObjectMapperBuilder.java:908) ~[spring-web-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.smile(Jackson2ObjectMapperBuilder.java:868) ~[spring-web-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter.<init>(MappingJackson2SmileHttpMessageConverter.java:50) ~[spring-web-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter.<init>(AllEncompassingFormHttpMessageConverter.java:90) ~[spring-web-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters(WebMvcConfigurationSupport.java:856) ~[spring-webmvc-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.getMessageConverters(WebMvcConfigurationSupport.java:811) ~[spring-webmvc-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.routerFunctionMapping(WebMvcConfigurationSupport.java:515) ~[spring-webmvc-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at com.qf.config.SwaggerConfig$$EnhancerBySpringCGLIB$$53e93b27.CGLIB$routerFunctionMapping$45(<generated>) ~[classes!/:1.0-SNAPSHOT]
        at com.qf.config.SwaggerConfig$$EnhancerBySpringCGLIB$$53e93b27$$FastClassBySpringCGLIB$$768dd8f3.invoke(<generated>) ~[classes!/:1.0-SNAPSHOT]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        at com.qf.config.SwaggerConfig$$EnhancerBySpringCGLIB$$53e93b27.routerFunctionMapping(<generated>) ~[classes!/:1.0-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_292]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_292]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_292]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_292]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar!/:5.2.5.RELEASE]
        ... 28 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.TSFBuilder
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_292]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_292]
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:92) ~[shops_manageNew-1.0-SNAPSHOT.jar:1.0-SNAPSHOT]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_292]

Question: nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/TSFBuilder

Solution: jackson imported from the pom.xml file depends on all comments. It is said on the Internet that jackson is built in springboot, which is incompatible with the imported version. Either comment or introduce a higher version; Here I have all the notes

Topics: Java Spring Spring Boot