-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathservlet-vs-jaxrs.json
More file actions
54 lines (54 loc) · 2.45 KB
/
servlet-vs-jaxrs.json
File metadata and controls
54 lines (54 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
"id": 98,
"slug": "servlet-vs-jaxrs",
"title": "Servlet versus JAX-RS",
"category": "enterprise",
"difficulty": "intermediate",
"jdkVersion": "11",
"oldLabel": "Java EE",
"modernLabel": "Jakarta EE 8+",
"oldApproach": "HttpServlet",
"modernApproach": "JAX-RS Resource",
"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}",
"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}",
"summary": "Replace verbose HttpServlet boilerplate with declarative JAX-RS resource classes.",
"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.",
"whyModernWins": [
{
"icon": "📐",
"title": "Declarative routing",
"desc": "Annotations define HTTP method, path, and content type instead of imperative if/else dispatch."
},
{
"icon": "🔄",
"title": "Automatic marshalling",
"desc": "Return POJOs directly; the runtime serialises them to JSON or XML based on @Produces."
},
{
"icon": "🧪",
"title": "Easier testing",
"desc": "Resource classes are plain Java objects, testable without a servlet container."
}
],
"support": {
"state": "available",
"description": "Widely available since Jakarta EE 8 / Java 11"
},
"prev": "language/compact-canonical-constructor",
"next": "enterprise/ejb-vs-cdi",
"related": [
"enterprise/ejb-vs-cdi",
"enterprise/jdbc-vs-jpa",
"io/http-client"
],
"docs": [
{
"title": "Jakarta RESTful Web Services Specification",
"href": "https://jakarta.ee/specifications/restful-ws/"
},
{
"title": "Jakarta REST 3.1 API",
"href": "https://jakarta.ee/specifications/restful-ws/3.1/apidocs/"
}
]
}