|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Hooking up OpenAPI with Jetty Server" |
| 4 | +data: 2021-06-12 06:038:56 |
| 5 | +categories: [java, jetty, swagger, openapi] |
| 6 | +--- |
| 7 | + |
| 8 | +### Background |
| 9 | + |
| 10 | +Jetty 11.X is released now and has deprecated its support for `javax` namespace required for the development of API. Instead, it has started supporting `jakarta` namespace now and this changes the game entirely. Because when you need to annotate your API, we `import javax.ws.rs.*` packages which is now on will be `import jakarta.ws.rs.*`. Nonetheless, swagger (OpenAPI) documentation that used to seamlessly integrate with core java APIs now has an additional dependency whether is a maven or gradle based project. Lets get started into the detailed process of how the new integration is done. |
| 11 | + |
| 12 | +### What is Jetty? |
| 13 | + |
| 14 | +The Ecplise Jetty Project provides a web server and servlet container, additionally providing support for HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These components are open source and are freely available for commercial use and distribution. |
| 15 | + |
| 16 | +Jetty is used in a wide variety of projects and products, both in development and production. Jetty has long been loved by developers due to its long history of being easily embedded in devices, tools, frameworks, application servers, and modern cloud services. |
| 17 | + |
| 18 | +Biggest advantage of using Jetty is that Jetty has been designed to have a small memory foot print. This is a critical basis for good performance and scalability. The less memory the server uses, the more memory is available for the application and/or more instances of the server can be run on virtual hardware. As such Jetty is very cloud friendly. |
| 19 | + |
| 20 | +### What is Jersey? |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +### What is Swagger/OpenAPI? |
| 25 | + |
| 26 | + |
| 27 | +### What steps it should follow? |
| 28 | + |
| 29 | +The rules are very simple and as follows: |
| 30 | +1. Create a jetty server |
| 31 | +2. Intercept API using Jersey |
| 32 | +3. Add OpenAPI servlet into Jetty Server |
| 33 | +4. Export API docs |
| 34 | +5. The class should not extend from any other Java class as in |
| 35 | + |
| 36 | +### STEP 1. Create Jetty Server |
| 37 | + |
| 38 | +```java |
| 39 | +``` |
| 40 | + |
| 41 | +### STEP 2. Intercept API using Jersey |
| 42 | + |
| 43 | +```java |
| 44 | +``` |
| 45 | + |
| 46 | +### STEP 3. Add OpenAPI servlet into Jetty Server |
| 47 | + |
| 48 | +```java |
| 49 | +``` |
| 50 | + |
| 51 | +Build.gradle should have dependencies like this - |
| 52 | + |
| 53 | +```gradle |
| 54 | +``` |
| 55 | + |
| 56 | +### STEP 4. Export API docs |
| 57 | +```java |
| 58 | +``` |
| 59 | + |
| 60 | +```java |
| 61 | +public class EmployeeRecord { |
| 62 | + /*the private data members inside a class that is public*/ |
| 63 | + private int empId; |
| 64 | + private String empName; |
| 65 | + private String empAddr; |
| 66 | + private int empSal; |
| 67 | + public int getEmpId() { |
| 68 | + return empId; |
| 69 | + } |
| 70 | + |
| 71 | + public void setEmpId(int idIn) { |
| 72 | + empId = idIn; |
| 73 | + } |
| 74 | + public String getEmpName() { |
| 75 | + return empName; |
| 76 | + } |
| 77 | + public void setEmpName(String nameIn) { |
| 78 | + empName = nameIn; |
| 79 | + } |
| 80 | + |
| 81 | + public String getEmpAddr() { |
| 82 | + return empAddr; |
| 83 | + } |
| 84 | + |
| 85 | + public void setEmpAddr(String addrIn) { |
| 86 | + empAddr = addrIn; |
| 87 | + } |
| 88 | + |
| 89 | + public int getEmpSal() { |
| 90 | + empSal; |
| 91 | + } |
| 92 | + |
| 93 | + public void setEmpSal(int salaryIn) { |
| 94 | + empSal = salaryIn; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | +### Summary |
| 99 | + |
| 100 | +In J2EE environment, POJO based programming model helps a lot to achieve the functionalities of dependency injection and inversion of control. Also this type of practice helps writing modules that are high cohesive and low coupled thereby making the project robust. Spring, one of the most popular Java framework in the industry always promotes the POJO based development. |
0 commit comments