Skip to content

Commit

Permalink
Update README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-brown committed Sep 14, 2024
1 parent 5fcf71e commit 3f476f0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ public void updateItem(
) throws SQLException { ... }
```

Like path parameters, body parameters are implicitly required. By default, content is assumed to be JSON and is automatically [converted](#type-coercion) to the specified type. However, subclasses can override the `decodeBody()` method to perform custom conversions.
Like path parameters, body parameters are implicitly required. If a handler method declares any query parameters, a body parameter must also be declared. A body parameter of type `Void` may be used to indicate that the handler either does not accept a body or will process the input stream directly, as discussed [below](#request-and-repsonse-properties).

By default, content is assumed to be JSON and is automatically [converted](#type-coercion) to the specified type. Subclasses can override the `decodeBody()` method to perform custom conversions.

### Return Values
Return values are converted to JSON as follows:
Expand Down Expand Up @@ -221,7 +223,7 @@ protected HttpServletRequest getRequest() { ... }
protected HttpServletResponse getResponse() { ... }
```

For example, a service might use the request to get the name of the current user, or use the response to return a custom header.
For example, a service might use the request to read directly from the input stream, or use the response to return a custom header.

The response object can also be used to produce a custom result. If a service method commits the response by writing to the output stream, the method's return value (if any) will be ignored by `WebService`. This allows a service to return content that cannot be easily represented as JSON, such as image data.

Expand Down Expand Up @@ -373,7 +375,7 @@ public interface ResponseHandler<T> {
}
```

If a service returns an error response, the default error handler will throw a `WebServiceException` (a subclass of `IOException`). If the type of the error response is "text/plain", the deserialized response body will be provided in the exception message.
If an operation does not complete successfully, the default error handler will throw a `WebServiceException` (a subclass of `IOException`). If the type of the error response is "text/plain", the response content will be provided in the exception message.

A custom error handler can be supplied via `setErrorHandler()`:

Expand Down
17 changes: 13 additions & 4 deletions kilo-server/src/main/java/org/httprpc/kilo/WebService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1117,11 +1117,20 @@ private Object[] getArguments(Parameter[] parameters, List<String> keys, Map<Str
}

if (n < parameters.length) {
try {
arguments[n] = decodeBody(request, parameters[n].getType());
} catch (IOException exception) {
throw new UnsupportedOperationException(exception);
var type = parameters[n].getType();

Object body;
if (type == Void.class) {
body = null;
} else {
try {
body = decodeBody(request, parameters[n].getType());
} catch (IOException exception) {
throw new UnsupportedOperationException(exception);
}
}

arguments[n] = body;
}

return arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
package org.httprpc.kilo.test;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.httprpc.kilo.RequestMethod;
import org.httprpc.kilo.ResourcePath;
import org.httprpc.kilo.beans.BeanAdapter;
import org.httprpc.kilo.io.CSVDecoder;
import org.httprpc.kilo.sql.QueryBuilder;

import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.SQLException;

@WebServlet(urlPatterns = {"/bulk-upload/*"}, loadOnStartup = 1)
Expand All @@ -38,11 +36,6 @@ public interface Row {

private static final int BATCH_SIZE = 25000;

@Override
protected Object decodeBody(HttpServletRequest request, Type type) {
return null;
}

@RequestMethod("POST")
@ResourcePath("upload")
public void upload(Void body) throws SQLException, IOException {
Expand Down
13 changes: 0 additions & 13 deletions kilo-test/src/main/java/org/httprpc/kilo/test/TestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import org.httprpc.kilo.Creates;
import org.httprpc.kilo.Description;
import org.httprpc.kilo.Name;
Expand All @@ -26,7 +25,6 @@
import org.httprpc.kilo.beans.BeanAdapter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.net.URL;
import java.time.DayOfWeek;
Expand Down Expand Up @@ -176,17 +174,6 @@ public BigInteger next() {
}
}

@Override
protected Object decodeBody(HttpServletRequest request, Type type) throws IOException {
var contentType = request.getContentType();

if (contentType != null && contentType.equals("application/octet-stream")) {
return null;
} else {
return super.decodeBody(request, type);
}
}

@RequestMethod("GET")
public Map<String, Object> testGet(@Required String string, List<String> strings,
Integer number, Set<Integer> numbers, boolean flag, char character, DayOfWeek dayOfWeek,
Expand Down

0 comments on commit 3f476f0

Please sign in to comment.