|
| 1 | +{ |
| 2 | + "id": 98, |
| 3 | + "slug": "servlet-vs-jaxrs", |
| 4 | + "title": "Servlet versus JAX-RS", |
| 5 | + "category": "enterprise", |
| 6 | + "difficulty": "intermediate", |
| 7 | + "jdkVersion": "11", |
| 8 | + "oldLabel": "Java EE", |
| 9 | + "modernLabel": "Jakarta EE 8+", |
| 10 | + "oldApproach": "HttpServlet", |
| 11 | + "modernApproach": "JAX-RS Resource", |
| 12 | + "oldCode": "@WebServlet(\"/users\")\npublic class UserServlet extends HttpServlet {\n @Override\n protected void doGet(HttpServletRequest req,\n HttpServletResponse res)\n throws ServletException, IOException {\n String id = req.getParameter(\"id\");\n res.setContentType(\"application/json\");\n res.getWriter().write(\"{\\\"id\\\":\\\"\" + id + \"\\\"}\");\n }\n}", |
| 13 | + "modernCode": "@Path(\"/users\")\npublic class UserResource {\n @GET\n @Produces(MediaType.APPLICATION_JSON)\n public Response getUser(\n @QueryParam(\"id\") String id) {\n return Response.ok(new User(id)).build();\n }\n}", |
| 14 | + "summary": "Replace verbose HttpServlet boilerplate with declarative JAX-RS resource classes.", |
| 15 | + "explanation": "JAX-RS (Jakarta RESTful Web Services) lets you expose REST endpoints using simple annotations like @GET, @Path, and @Produces. No more manual parsing of request parameters or setting content types on the response — the runtime handles marshalling and routing automatically.", |
| 16 | + "whyModernWins": [ |
| 17 | + { |
| 18 | + "icon": "📐", |
| 19 | + "title": "Declarative routing", |
| 20 | + "desc": "Annotations define HTTP method, path, and content type instead of imperative if/else dispatch." |
| 21 | + }, |
| 22 | + { |
| 23 | + "icon": "🔄", |
| 24 | + "title": "Automatic marshalling", |
| 25 | + "desc": "Return POJOs directly; the runtime serialises them to JSON or XML based on @Produces." |
| 26 | + }, |
| 27 | + { |
| 28 | + "icon": "🧪", |
| 29 | + "title": "Easier testing", |
| 30 | + "desc": "Resource classes are plain Java objects, testable without a servlet container." |
| 31 | + } |
| 32 | + ], |
| 33 | + "support": { |
| 34 | + "state": "available", |
| 35 | + "description": "Widely available since Jakarta EE 8 / Java 11" |
| 36 | + }, |
| 37 | + "prev": "language/compact-canonical-constructor", |
| 38 | + "next": "enterprise/ejb-vs-cdi", |
| 39 | + "related": [ |
| 40 | + "enterprise/ejb-vs-cdi", |
| 41 | + "enterprise/jdbc-vs-jpa", |
| 42 | + "io/http-client" |
| 43 | + ], |
| 44 | + "docs": [ |
| 45 | + { |
| 46 | + "title": "Jakarta RESTful Web Services Specification", |
| 47 | + "href": "https://jakarta.ee/specifications/restful-ws/" |
| 48 | + }, |
| 49 | + { |
| 50 | + "title": "Jakarta REST 3.1 API", |
| 51 | + "href": "https://jakarta.ee/specifications/restful-ws/3.1/apidocs/" |
| 52 | + } |
| 53 | + ] |
| 54 | +} |
0 commit comments