Skip to content

Commit 8be0239

Browse files
author
mendrika261
committed
sprint14: JSON response with annotation @RESTapi
1 parent fe70071 commit 8be0239

8 files changed

Lines changed: 38 additions & 7 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package etu2024.framework.annotation;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
public @interface RestAPI {
8+
}

.framework/dev_files/etu2024/framework/servlet/FrontServlet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.Gson;
44
import etu2024.framework.annotation.Auth;
5+
import etu2024.framework.annotation.RestAPI;
56
import etu2024.framework.annotation.Session;
67
import etu2024.framework.annotation.Singleton;
78
import etu2024.framework.core.File;
@@ -80,6 +81,11 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
8081
return;
8182
}
8283

84+
if(method.isAnnotationPresent(RestAPI.class)) {
85+
printJson(method.invoke(object, parameters.toArray()), response);
86+
return;
87+
}
88+
8389
ModelView modelView = (ModelView) method.invoke(object, parameters.toArray());
8490

8591
if(method.isAnnotationPresent(Session.class) || objectClass.isAnnotationPresent(Session.class) ||

.framework/framework.jar

426 Bytes
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package etu2024.framework.annotation;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
@Retention(RetentionPolicy.RUNTIME)
7+
public @interface RestAPI {
8+
}

framework/src/main/java/etu2024/framework/servlet/FrontServlet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.Gson;
44
import etu2024.framework.annotation.Auth;
5+
import etu2024.framework.annotation.RestAPI;
56
import etu2024.framework.annotation.Session;
67
import etu2024.framework.annotation.Singleton;
78
import etu2024.framework.core.File;
@@ -80,6 +81,11 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
8081
return;
8182
}
8283

84+
if(method.isAnnotationPresent(RestAPI.class)) {
85+
printJson(method.invoke(object, parameters.toArray()), response);
86+
return;
87+
}
88+
8389
ModelView modelView = (ModelView) method.invoke(object, parameters.toArray());
8490

8591
if(method.isAnnotationPresent(Session.class) || objectClass.isAnnotationPresent(Session.class) ||
329 Bytes
Binary file not shown.

test-framework/src/main/java/Emp.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import etu2024.framework.annotation.Auth;
2+
import etu2024.framework.annotation.RestAPI;
3+
import etu2024.framework.annotation.Session;
24
import etu2024.framework.core.File;
35
import etu2024.framework.core.ModelView;
46
import etu2024.framework.annotation.Url;
57

68
import java.sql.Date;
7-
import java.util.Arrays;
89

910
public class Emp {
10-
String[] name;
11+
String name;
1112
Date creation;
1213
File file;
1314

@@ -37,12 +38,14 @@ public ModelView logout() {
3738
return modelView;
3839
}
3940

41+
@RestAPI
42+
@Session
4043
@Url(url = "/find")
41-
public ModelView get(Double id) {
42-
ModelView modelView = new ModelView("test.jsp");
43-
modelView.setJson(true);
44-
modelView.addItem("name", "test");
45-
return modelView;
44+
public Emp get(Double id) {
45+
Emp emp = new Emp();
46+
emp.setCreation(new Date(0));
47+
emp.name = "";
48+
return emp;
4649
}
4750

4851
public Date getCreation() {
426 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)