Integrate Spring Boot in an EAR project

前端 未结 3 1305
傲寒
傲寒 2021-01-02 19:55

I have an existing war project created using spring boot. How to package it within an EAR which has an EJB module?

Is there any way to move the model and dao package

3条回答
  •  死守一世寂寞
    2021-01-02 20:08

    I have created a multi module gradle project including a spring RESTFul web services - EAR application name is - bluestone bluestone/settings.gradle -

    rootProject.name = 'bluestone'
    include ':bluestone-web'
    include ':bluestone-core'
    include ':bluestone-rest'
    
    project (':bluestone-web').projectDir = new File('bluestone-web')
    project (':bluestone-core').projectDir = new File('bluestone-core')
    project (':bluestone-rest').projectDir = new File('bluestone-rest')

    bluestone-rest project structure is -

    bluestone-rest/build.gradle

    plugins {
        id 'war'
    }
    
    group 'com.bluestone.smart.rest'
    version '1.0-SNAPSHOT'
    
    
    
    dependencies {
        compile library.spring_context
        compile library.spring_web
        compile library.spring_beans
        compile library.spring_mvc
        providedCompile library.servlet_api
        testCompile library.junit
    
    }

    all the dependencies are imported from common libraries.gradle. common libraries.gradle is user ear bluestone/libraries.gradle

    /* ============================================================================
       Library definitions for project 'Bluestone'
       ============================================================================
       Define here library dependencies and use them inside sub-modules build.gradle.
    
       Included from: "${rootProject.projectDir}/build.gradle"
       ============================================================================
    
     */
    ext {
    
        library = [
                /* testing */
                junit: "junit:junit:4.12",
                log4j: "log4j:log4j:1.2.17",
    
                /* Spring libraries*/
                spring_context:					"org.springframework:spring-context:${spring_lib_version}",
                spring_aop:						"org.springframework:spring-aop:${spring_lib_version}",
                spring_beans:					"org.springframework:spring-beans:${spring_lib_version}",
                spring_orm:						"org.springframework:spring-orm:${spring_lib_version}",
                spring_web:						"org.springframework:spring-web:${spring_lib_version}",
                spring_mvc:                     "org.springframework:spring-webmvc:${spring_lib_version}",
                servlet_api:                     "javax.servlet:javax.servlet-api:4.0.1"
    
        ]
    }

    Within bluestone-rest, i have created three basic file to test a sample rest message -

    1. spring Configuration named - BlueRestConfiguration.java

    package com.bluestone.smart.rest.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScans;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = {"com.bluestone.smart.rest.resources", "com.bluestone.smart.rest.controller"})
    public class BlueRestConfiguration {
    }

    1. Initialization file - named is - RestInit.java

    package com.bluestone.smart.rest.config;
    
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    import javax.servlet.ServletContext;
    
    public class RestInit extends AbstractAnnotationConfigDispatcherServletInitializer {
        /**
         * Specify {@code @Configuration} and/or {@code @Component} classes for the
         * {@linkplain #createRootApplicationContext() root application context}.
         *
         * @return the configuration for the root application context, or {@code null}
         * if creation and registration of a root context is not desired
         */
        @Override
        protected Class[] getRootConfigClasses() {
            return new Class[] {BlueRestConfiguration.class};
        }
    
        /**
         * Specify {@code @Configuration} and/or {@code @Component} classes for the
         * {@linkplain #createServletApplicationContext() Servlet application context}.
         *
         * @return the configuration for the Servlet application context, or
         * {@code null} if all configuration is specified through root config classes.
         */
        @Override
        protected Class[] getServletConfigClasses() {
            return null;
        }
    
        /**
         * Specify the servlet mapping(s) for the {@code DispatcherServlet} —
         * for example {@code "/"}, {@code "/app"}, etc.
         *
         * @see #registerDispatcherServlet(ServletContext)
         */
        @Override
        protected String[] getServletMappings() {
            return new String[] {"/"};
        }
    }

    1. A rest service API - named - GreetingsController.java

    package com.bluestone.smart.rest.resources;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class GreetingsController {
        @RequestMapping(path = "/greeting", method = RequestMethod.GET)
        public String greetings(){
            return "Welcome Spring Rest!";
        }
    }

    finally build this EAR application using -

    gradlew clean build 

    and deploy on WildFly application and then calling this service using postman -

    Please let me know if any more details required. I shall push this code on git and will share the git link here.

提交回复
热议问题