May 20th, 2023
Microservices with Modules(JPMS)
4.0.0
com.packtpub.mmj
6392_chapter2
1.0-SNAPSHOT
pom
lib
rest
UTF-8
9
9
2.0.0.BUILD-SNAPSHOT
5.0.0.BUILD-SNAPSHOT
com.packtpub.mmj.rest.RestSampleApp
org.springframework.boot
spring-boot-starter-parent
2.0.0.BUILD-SNAPSHOT
com.pactkpub.mmj
rest
${project.version}
com.packtpub.mmj
lib
${project.version}
org.springframework.boot
spring-boot-maven-plugin
2.0.0.BUILD-SNAPSHOT
repackage
exec
${start-class}
org.apache.maven.plugins
maven-compiler-plugin
3.6.1
1.9
true
true
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
4.0.0
com.packtpub.mmj
6392_chapter2
1.0-SNAPSHOT
lib
jar
com.packtpub.mmj.lib.Lib
// 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 input;
private List output;
public Calculation(){
}
public Calculation(List input, List output, String function) {
this.function = function;
this.input = input;
this.output = output;
}
public List getInput() {
return input;
}
public void setInput(List input) {
this.input = input;
}
public List getOutput() {
return output;
}
public void setOutput(List output) {
this.output = output;
}
public String getFunction() {
return function;
}
public void setFunction(String function) {
this.function = function;
}
}
4.0.0
com.packtpub.mmj
6392_chapter2
1.0-SNAPSHOT
rest
jar
com.packtpub.mmj.rest.Rest
com.packtpub.mmj
lib
org.springframework.boot
spring-boot-starter-web
// 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 input = new ArrayList();
input.add(b);
input.add(e);
List 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 input = new ArrayList<>();
input.add(aValue);
List 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.(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