Skip to content

Commit d824312

Browse files
committed
Update readme doc
1 parent 2a91522 commit d824312

1 file changed

Lines changed: 2 additions & 236 deletions

File tree

README.md

Lines changed: 2 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Request Parsing Framework
22

3-
Welcome to the **Request Parsing Framework**! This framework offers a streamlined approach to handling and validating HTTP requests in Java applications using custom annotations. By leveraging reflection and annotations, it simplifies the process of mapping request parameters to Java objects, supporting various data types, default values, and validation mechanisms.
3+
Welcome to the Request Parsing Framework! This framework offers a streamlined approach to handling and validating HTTP requests in **Java servlet-based applications** that do not utilize Spring Boot's extensive support tools. By leveraging reflection and custom annotations, it simplifies the process of mapping request parameters to Java objects, supporting various data types, default values, and validation mechanisms.
44

55
## Table of Contents
66

@@ -15,11 +15,7 @@ Welcome to the **Request Parsing Framework**! This framework offers a streamline
1515
- [Classes](#classes)
1616
- [`BodyParser`](#bodyparser)
1717
- [`RequestParser`](#requestparser)
18-
- [Usage](#usage)
19-
- [Defining Data Classes](#defining-data-classes)
20-
- [Parsing Requests](#parsing-requests)
2118
- [Testing](#testing)
22-
- [Example](#example)
2319
- [Contributing](#contributing)
2420
- [License](#license)
2521

@@ -162,8 +158,6 @@ The `BodyParser` class is responsible for parsing HTTP request bodies and mappin
162158
- `parse(HttpServletRequest request, Class<T> clazz)`: Parses the request and populates an instance of the specified class.
163159
- `parseToJSONObject(InputStream inputStream)`: Reads the request body and parses it into a `JSONObject`.
164160

165-
**Implementation:**
166-
167161
#### Usage Example
168162

169163
```java
@@ -250,14 +244,13 @@ UserInfo{name='Alice', age=25, tags=[bac, java, python], scores=[80, 90, 85], ra
250244

251245
#### `RequestParser`
252246

253-
The `RequestParser` class provides static methods to parse HTTP request parameters into Java objects using the `@RequestParam` annotation. It offers additional flexibility and utility functions for handling different parameter types.
247+
The `RequestParser` class provides static methods to parse HTTP request parameters into Java objects using the `@RequestParam` annotation. It offers additional flexibility and utility functions for handling different parameter types. For handle body from `x-www-form-urlencoded`, use `RequestParser` class.
254248

255249
**Key Methods:**
256250

257251
- `parseRequest(HttpServletRequest req, Class<T> clazz)`: Parses the request parameters and maps them to an instance of the specified class.
258252
- `parseParamToList(HttpServletRequest req, String paramName, String defaultValue, boolean isRequired, Class<T> listType)`: Parses a specific parameter into a `List` of the desired type.
259253

260-
**Implementation:**
261254

262255
#### Usage Example
263256

@@ -368,104 +361,6 @@ public class ExampleUsageWithRequestParser {
368361
UserInfo{name='Alice', age=25, tags=[bac, java, python], scores=[80, 90, 85], ratings=[4.5, 3.8, 5.0], metrics=[0.75, 0.85, 0.95]}
369362
```
370363

371-
## Usage
372-
373-
### Defining Data Classes
374-
375-
Define your data classes with fields annotated using `@RequestParam` to specify how they should be populated from HTTP request parameters.
376-
377-
**Example:**
378-
379-
```java
380-
import com.openext.dev.annotations.RequestParam;
381-
import lombok.Getter;
382-
import lombok.Setter;
383-
import java.util.List;
384-
385-
@Getter
386-
@Setter
387-
public class UserInfo {
388-
@RequestParam(name = "name", required = true)
389-
private String name;
390-
391-
@RequestParam(name = "age", required = true)
392-
private int age;
393-
394-
@RequestParam(name = "tags", required = false, defaultValue = "bac,java,python", message = "Tags are required")
395-
private List<String> tags;
396-
397-
@RequestParam(name = "scores", required = false, defaultValue = "80,90,85", message = "Scores are required")
398-
private List<Integer> scores;
399-
400-
@RequestParam(name = "ratings", required = false, defaultValue = "4.5,3.8,5.0", message = "Ratings are required")
401-
private List<Float> ratings;
402-
403-
@RequestParam(name = "metrics", required = false, defaultValue = "0.75,0.85,0.95", message = "Metrics are required")
404-
private List<Double> metrics;
405-
406-
@Override
407-
public String toString() {
408-
return "UserInfo{" +
409-
"name='" + name + '\'' +
410-
", age=" + age +
411-
", tags=" + tags +
412-
", scores=" + scores +
413-
", ratings=" + ratings +
414-
", metrics=" + metrics +
415-
'}';
416-
}
417-
}
418-
```
419-
420-
### Parsing Requests
421-
422-
Use the `BodyParser` or `RequestParser` class to parse incoming HTTP requests and map the parameters to your data classes.
423-
424-
**Using `BodyParser`:**
425-
426-
```java
427-
import javax.servlet.http.HttpServletRequest;
428-
import org.json.JSONObject;
429-
430-
public class ExampleUsage {
431-
private BodyParser bodyParser = new BodyParser();
432-
433-
public void handleRequest(HttpServletRequest request) {
434-
try {
435-
// Parse request parameters into UserInfo object
436-
UserInfo userInfo = bodyParser.parse(request, UserInfo.class);
437-
System.out.println(userInfo);
438-
439-
// Alternatively, parse the request body into a JSONObject
440-
JSONObject jsonObject = bodyParser.parseToJSONObject(request.getInputStream());
441-
System.out.println(jsonObject.toString(2)); // Pretty print JSON
442-
} catch (MissingParameterException | IOException | IllegalAccessException | JSONException e) {
443-
e.printStackTrace();
444-
// Handle exceptions appropriately
445-
}
446-
}
447-
}
448-
```
449-
450-
**Using `RequestParser`:**
451-
452-
```java
453-
import javax.servlet.http.HttpServletRequest;
454-
455-
public class ExampleUsageWithRequestParser {
456-
public void handleRequest(HttpServletRequest request) {
457-
try {
458-
// Parse request parameters into UserInfo object using RequestParser
459-
UserInfo userInfo = RequestParser.parseRequest(request, UserInfo.class);
460-
System.out.println(userInfo);
461-
} catch (IllegalArgumentException e) {
462-
e.printStackTrace();
463-
// Handle exceptions appropriately
464-
}
465-
}
466-
}
467-
```
468-
469364
## Testing
470365

471366
The framework includes comprehensive unit tests to ensure reliability and correctness. Tests are written using JUnit 5 and Mockito.
@@ -887,135 +782,6 @@ public class RequestParserTest {
887782
}
888783
```
889784

890-
## Example
891-
892-
### Defining the `@RequestParam` Annotation
893-
894-
```java
895-
package com.openext.dev.annotations;
896-
897-
import java.lang.annotation.*;
898-
899-
@Retention(RetentionPolicy.RUNTIME)
900-
@Target(ElementType.FIELD)
901-
public @interface RequestParam {
902-
String name();
903-
boolean required() default false;
904-
String defaultValue() default "";
905-
String message() default "";
906-
}
907-
```
908-
909-
### Defining the `UserInfo` Class
910-
911-
```java
912-
package com.openext.dev;
913-
914-
import com.openext.dev.annotations.RequestParam;
915-
import lombok.Getter;
916-
import lombok.Setter;
917-
import java.util.List;
918-
919-
@Getter
920-
@Setter
921-
public class UserInfo {
922-
@RequestParam(name = "name", required = true)
923-
private String name;
924-
925-
@RequestParam(name = "age", required = true)
926-
private int age;
927-
928-
@RequestParam(name = "tags", required = false, defaultValue = "bac,java,python", message = "Tags are required")
929-
private List<String> tags;
930-
931-
@RequestParam(name = "scores", required = false, defaultValue = "80,90,85", message = "Scores are required")
932-
private List<Integer> scores;
933-
934-
@RequestParam(name = "ratings", required = false, defaultValue = "4.5,3.8,5.0", message = "Ratings are required")
935-
private List<Float> ratings;
936-
937-
@RequestParam(name = "metrics", required = false, defaultValue = "0.75,0.85,0.95", message = "Metrics are required")
938-
private List<Double> metrics;
939-
940-
@Override
941-
public String toString() {
942-
return "UserInfo{" +
943-
"name='" + name + '\'' +
944-
", age=" + age +
945-
", tags=" + tags +
946-
", scores=" + scores +
947-
", ratings=" + ratings +
948-
", metrics=" + metrics +
949-
'}';
950-
}
951-
}
952-
```
953-
954-
### Parsing an HTTP Request with `BodyParser`
955-
956-
```java
957-
import javax.servlet.http.HttpServletRequest;
958-
import org.json.JSONObject;
959-
960-
public class ExampleUsage {
961-
private BodyParser bodyParser = new BodyParser();
962-
963-
public void handleRequest(HttpServletRequest request) {
964-
try {
965-
// Parse request parameters into UserInfo object
966-
UserInfo userInfo = bodyParser.parse(request, UserInfo.class);
967-
System.out.println(userInfo);
968-
969-
// Alternatively, parse the request body into a JSONObject
970-
JSONObject jsonObject = bodyParser.parseToJSONObject(request.getInputStream());
971-
System.out.println(jsonObject.toString(2)); // Pretty print JSON
972-
} catch (MissingParameterException | IOException | IllegalAccessException | JSONException e) {
973-
e.printStackTrace();
974-
// Handle exceptions appropriately
975-
}
976-
}
977-
}
978-
```
979-
980-
**Output:**
981-
982-
```
983-
UserInfo{name='Alice', age=25, tags=[bac, java, python], scores=[80, 90, 85], ratings=[4.5, 3.8, 5.0], metrics=[0.75, 0.85, 0.95]}
984-
{
985-
"name": "Alice",
986-
"age": 25,
987-
"hobbies": [
988-
"reading",
989-
"swimming"
990-
]
991-
}
992-
```
993-
994-
### Parsing an HTTP Request with `RequestParser`
995-
996-
```java
997-
import javax.servlet.http.HttpServletRequest;
998-
999-
public class ExampleUsageWithRequestParser {
1000-
public void handleRequest(HttpServletRequest request) {
1001-
try {
1002-
// Parse request parameters into UserInfo object using RequestParser
1003-
UserInfo userInfo = RequestParser.parseRequest(request, UserInfo.class);
1004-
System.out.println(userInfo);
1005-
} catch (IllegalArgumentException e) {
1006-
e.printStackTrace();
1007-
// Handle exceptions appropriately
1008-
}
1009-
}
1010-
}
1011-
```
1012-
1013-
**Output:**
1014-
1015-
```
1016-
UserInfo{name='Alice', age=25, tags=[bac, java, python], scores=[80, 90, 85], ratings=[4.5, 3.8, 5.0], metrics=[0.75, 0.85, 0.95]}
1017-
```
1018-
1019785
## Contributing
1020786

1021787
We welcome contributions to the **Request Parsing Framework**! Please follow these steps to contribute:

0 commit comments

Comments
 (0)