May 20th, 2023
Microservices with Modules(JPMS)

 

				
					<!-- pom.xml in 6392_chapter2 -->
<?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.packtpub.mmj</groupId>
    <artifactId>6392_chapter2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>lib</module>
        <module>rest</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
        <spring-boot-version>2.0.0.BUILD-SNAPSHOT</spring-boot-version>
        <spring-version>5.0.0.BUILD-SNAPSHOT</spring-version>
        <start-class>com.packtpub.mmj.rest.RestSampleApp</start-class>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.pactkpub.mmj</groupId>
                <artifactId>rest</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.packtpub.mmj</groupId>
                <artifactId>lib</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.BUILD-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                            <mainClass>${start-class}</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.9</source>
                    <target>1.9</target>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>
				
			
				
					<!-- pom.xml in lib -->
<?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>
    <parent>
        <groupId>com.packtpub.mmj</groupId>
        <artifactId>6392_chapter2</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>lib</artifactId>
    <packaging>jar</packaging>
    <properties>
        <exec.mainClass>com.packtpub.mmj.lib.Lib</exec.mainClass>
    </properties>
</project>
				
			
				
					// module-info.java in lib module
module com.packtpub.mmj.lib {
    exports com.packtpub.mmj.lib.model to com.packtpub.mmj.rest;
    opens com.packtpub.mmj.lib.model;
}
				
			
				
					// Calculation.java
package com.packtpub.mmj.lib.model;

import java.util.List;

public class Calculation {
    
    String function;
    private List<String> input;
    private List<String> output;
    
    public Calculation(){
        
    }
    public Calculation(List<String> input, List<String> output, String function) {
        this.function = function;
        this.input = input;
        this.output = output;
    }
    public List<String> getInput() {
        return input;
    }
    public void setInput(List<String> input) {
        this.input = input;
    }
    public List<String> getOutput() {
        return output;
    }
    public void setOutput(List<String> output) {
        this.output = output;
    }
    public String getFunction() {
        return function;
    }
    public void setFunction(String function) {
        this.function = function;
    }    
}

				
			
				
					<!-- pom.xml in rest -->
<?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>
    <parent>
        <groupId>com.packtpub.mmj</groupId>
        <artifactId>6392_chapter2</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>rest</artifactId>
    <packaging>jar</packaging>
    <properties>
        <exec.mainClass>com.packtpub.mmj.rest.Rest</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.packtpub.mmj</groupId>
            <artifactId>lib</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
				
			
				
					// module-info.java in rest module
module com.packtpub.mmj.rest {
    requires spring.core;
    requires spring.beans;
    requires spring.context;
    requires spring.aop;
    requires spring.web;
    requires spring.expression;

    requires spring.boot;
    requires spring.boot.autoconfigure;

    requires com.packtpub.mmj.lib;

    exports com.packtpub.mmj.rest;
    exports com.packtpub.mmj.rest.resources;

    opens com.packtpub.mmj.rest;
    opens com.packtpub.mmj.rest.resources;
}

				
			
				
					// CalculationController.java
package com.packtpub.mmj.rest.resources;

import com.packtpub.mmj.lib.model.Calculation;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("calculation")
public class CalculationController {
    private static final String PATTERN = "^-?+\\d+\\.?+\\d*$";

    @RequestMapping("/power")
    public Calculation pow(@RequestParam(value = "base") String b, @RequestParam(value = "exponent") String e) {
        List<String> input = new ArrayList();
        input.add(b);
        input.add(e);
        List<String> output = new ArrayList();
        String powValue;
        if (b != null && e != null && b.matches(PATTERN) && e.matches(PATTERN)) {
            powValue = String.valueOf(Math.pow(Double.valueOf(b), Double.valueOf(e)));
        } else {
            powValue = "Base or/and Exponent is/are not set to numeric value.";
        }
        output.add(powValue);
        return new Calculation(input, output, "power");
    }

    @RequestMapping(value = "/sqrt/{value:.+}", method = RequestMethod.GET)
    public Calculation sqrt(@PathVariable(value = "value") String aValue) {
        List<String> input = new ArrayList<>();
        input.add(aValue);
        List<String> output = new ArrayList();
        String sqrtValue;
        if (aValue != null && aValue.matches(PATTERN)) {
            sqrtValue = String.valueOf(Math.sqrt(Double.valueOf(aValue)));
        } else {
            sqrtValue = "Input value is not set to numeric value.";
        }
        output.add(sqrtValue);
        return new Calculation(input, output, "sqrt");
    }
}

				
			
				
					//RestSampleApp.java
package com.packtpub.mmj.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

				
			
				
					> mvn clean install
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for 6392_chapter2 1.0-SNAPSHOT:
[INFO] 
[INFO] 6392_chapter2 ...................................... SUCCESS [  0.725 s]
[INFO] lib ................................................ SUCCESS [  1.216 s]
[INFO] rest ............................................... SUCCESS [  0.806 s]

				
			
				
					//While using JDK 16 and above, we get an error similar to this
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @198e2867
        at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:387) ~[na:na]
        at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:363) ~[na:na]
        at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:311) ~[na:na]
        at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:201) ~[na:na]
        at java.base/java.lang.reflect.Method.setAccessible(Method.java:195) ~[na:na]
        at org.springframework.cglib.core.ReflectUtils$1.run(ReflectUtils.java:61) ~[spring-core-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:569) ~[na:na]
        at org.springframework.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:52) ~[spring-core-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
        at org.springframework.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:243) ~[spring-core-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
        at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) ~[spring-core-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
        at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329) ~[spring-core-5.0.4.RELEASE.jar!/:5.0.4.RELEASE]
        ... 33 common frames omitted
				
			
				
					// use this code to run without the above errors
java --add-opens java.base/java.lang=ALL-UNNAMED -jar rest/target/rest-1.0-SNAPSHOT-exec.jar
				
			
				
					// run the code on different port
java --add-opens java.base/java.lang=ALL-UNNAMED -Dserver.port=8083 -jar rest/target/rest-1.0-SNAPSHOT-exec.jar
				
			

http://localhost:8083/calculation/power?base=2&exponent=3

http://localhost:8083/calculation/sqrt/2

 

Leave a Reply

Your email address will not be published. Required fields are marked *