Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Spring Boot Distributed Project Setup with Maven and IntelliJ IDEA

Tech May 18 2

Project Initialization Using Maven in IntelliJ IDEA

Start by creating a new Maven project named bos2 in IntelliJ IDEA. Once the project is created, remove the default src directory since this will serve as a parenet (aggregator) module for multiple sub-modules.

Configure Parent POM

Update the root pom.xml to define the project as a multi-module build with packaging type pom. This file also sets shared dependencies and versions across modules.

<packaging>pom</packaging>

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.li</groupId>
    <artifactId>bos2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>crm_domain</module>
        <module>crm_management</module>
    </modules>
    <packaging>pom</packaging>
    <name>bos2.0</name>
    <description>BOS v2.0 Integrated Platform</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>

        <spring-data-jpa.version>1.9.0.RELEASE</spring-data-jpa.version>
        <elasticsearch.version>2.4.6</elasticsearch.version>
        <jackson.version>2.8.5</jackson.version>
        <boot-starter-jaxrs.version>3.2.0</boot-starter-jaxrs.version>
        <jna.version>3.0.9</jna.version>
        <ojdbc6.version>11.2.0.1.0</ojdbc6.version>
        <shiro.version>1.4.0</shiro.version>
        <pinyin4j.version>2.5.0</pinyin4j.version>
        <poi.version>3.11</poi.version>
        <httpclient.version>4.5.3</httpclient.version>
        <fastjson.version>1.2.8</fastjson.version>
        <mail.version>1.4.7</mail.version>
        <commons-lang.version>2.6</commons-lang.version>
        <commons-io.version>1.3.2</commons-io.version>
        <itext.version>5.2.0</itext.version>
        <quartz.version>2.2.1</quartz.version>
        <mysql.version>5.1.35</mysql.version>
    </properties>
</project>

Create Domain Module (crm_domain)

Right-click on the root project and create a new Maven module named crm_domain. This module will hold JPA entities used across services.

crm_domain's pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>bos2</artifactId>
        <groupId>com.li</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>crm_domain</artifactId>
    <name>crm_domain</name>
    <description>Domain entities for Customer Relationship Management</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

Define Entity: Customer

Create a JPA entity representing customer data mapped to the database table T_CUSTOMER.

package top.kylewang.crm.domain;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

@Entity
@Table(name = "T_CUSTOMER")
@XmlRootElement(name = "customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "C_ID")
    private Integer id;

    @Column(name = "C_USERNAME")
    private String username;

    @Column(name = "C_PASSWORD")
    private String password;

    @Column(name = "C_TYPE")
    private Integer type;

    @Column(name = "C_BRITHDAY")
    @Temporal(TemporalType.DATE)
    private Date birthday;

    @Column(name = "C_SEX")
    private Integer sex;

    @Column(name = "C_TELEPHONE")
    private String telephone;

    @Column(name = "C_COMPANY")
    private String company;

    @Column(name = "C_DEPARTMENT")
    private String department;

    @Column(name = "C_POSITION")
    private String position;

    @Column(name = "C_ADDRESS")
    private String address;

    @Column(name = "C_MOBILEPHONE")
    private String mobilePhone;

    @Column(name = "C_EMAIL")
    private String email;

    @Column(name = "C_Fixed_AREA_ID")
    private String fixedAreaId;

    // Getters and Setters
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id; }

    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }

    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    public Integer getType() { return type; }
    public void setType(Integer type) { this.type = type; }

    public Date getBirthday() { return birthday; }
    public void setBirthday(Date birthday) { this.birthday = birthday; }

    public Integer getSex() { return sex; }
    public void setSex(Integer sex) { this.sex = sex; }

    public String getTelephone() { return telephone; }
    public void setTelephone(String telephone) { this.telephone = telephone; }

    public String getCompany() { return company; }
    public void setCompany(String company) { this.company = company; }

    public String getDepartment() { return department; }
    public void setDepartment(String department) { this.department = department; }

    public String getPosition() { return position; }
    public void setPosition(String position) { this.position = position; }

    public String getAddress() { return address; }
    public void setAddress(String address) { this.address = address; }

    public String getMobilePhone() { return mobilePhone; }
    public void setMobilePhone(String mobilePhone) { this.mobilePhone = mobilePhone; }

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    public String getFixedAreaId() { return fixedAreaId; }
    public void setFixedAreaId(String fixedAreaId) { this.fixedAreaId = fixedAreaId; }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", username=" + username + ", password=" + password +
               ", type=" + type + ", birthday=" + birthday + ", sex=" + sex +
               ", telephone=" + telephone + ", company=" + company +
               ", department=" + department + ", position=" + position +
               ", address=" + address + ", mobilePhone=" + mobilePhone +
               ", email=" + email + ", fixedAreaId=" + fixedAreaId + "]";
    }
}

Create Service Module: crm_management

Create another submodule called crm_management that depends on crm_domain and provides RESTful APIs.

crm_management's pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>bos2</artifactId>
        <groupId>com.li</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>crm_management</artifactId>
    <packaging>jar</packaging>
    <name>crm_management</name>
    <description>CRM Management System Backend</description>

    <dependencies>
        <dependency>
            <groupId>com.li</groupId>
            <artifactId>crm_domain</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>${boot-starter-jaxrs.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>${commons-lang.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
    </dependencies>

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

Application Configuration via application.yml

Add an application.yml file under src/main/resources to configure server settings, database connection, JPA, and CXF endpoints.

server:
  port: 9002
  context-path: /crm

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/crm
    username: root
    password: 136735
  jpa:
    show-sql: true
  jackson:
    default-property-inclusion: non_null
  devtools:
    restart:
      enabled: true

cxf:
  path: /services
  servlet.init:
    service-list-path: /info
  jaxrs:
    component-scan: true

Main Application Class and Controller

Create the Sprinng Boot main class with JAX-RS support enabled through component scanning.

package top.kylewang.crm;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class CrmManagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrmManagementApplication.class, args);
    }

    @Bean
    public JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {
        return new JacksonJaxbJsonProvider();
    }
}

Add a simple test controller to verify the setup:

package top.kylewang.crm.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/hello")
    public String sayHello() {
        return "hello";
    }
}

Run and Test the Application

Launch the CrmManagementApplication class. After startup, navigate to:

http://localhost:9002/crm/hello

You should see the response: hello, confirming the successful deployment of your distributed Spring Boot service.

Tags: spring-boot

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.