<properties>
<tomcat.version>9.0.43</tomcat.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
Plain tomcat is lightweight without spring dependencies. For building microservices, we can embed tomcat into a java project.
Write plain old servlets and filters to handle REST requests.
Spring boot provides RequestMapping to handle JSON requests. The incoming json payload can be accepted as string body and converted to JSON using a JSON parsing library.
String requestData = request.getReader().lines().collect(Collectors.joining());
The incoming json/xml request can be converted to object using jaxb. Here Moxy can come handy to do what spring boot is doing with @RequestMapping and @RequestBody annotations. In the below piece of code, you can pass the request data as stream
and class name to marshal the object to.
public Object getBodyObject(InputStream stream, Class clazz) {
try {
Map<String, Object> properties = new HashMap<>();
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
final JAXBContext jaxbContext =
JAXBContextFactory.createContext(new Class<?>[]{clazz}, properties);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
//https://stackoverflow.com/q/20962053/1097600
return unmarshaller.unmarshal(new StreamSource(stream), clazz).getValue();
} catch (JAXBException e) {
Logger.getLogger(JsonBodyStrategy.class.getName()).log(Level.SEVERE, null, e);
}
return null;
}
JAXB can be used to convert the Object back to json during response.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.6.0</version>
</dependency>
```xml
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
</dependency>