Package ratpack.core.parse
Class NoOptParserSupport
- java.lang.Object
-
- ratpack.core.parse.ParserSupport<Void>
-
- ratpack.core.parse.NoOptParserSupport
-
public abstract class NoOptParserSupport extends ParserSupport<Void>
A convenience base for parsers that don't require options.The following is an example of an implementation that parses to an
Integer
.import com.google.common.reflect.TypeToken; import ratpack.exec.Promise; import ratpack.core.handling.Context; import ratpack.core.handling.Handler; import ratpack.core.http.TypedData; import ratpack.core.parse.NoOptParserSupport; import ratpack.test.handling.HandlingResult; import ratpack.test.handling.RequestFixture; import ratpack.func.Types; import static org.junit.jupiter.api.Assertions.assertEquals; public class Example { public static class IntParser extends NoOptParserSupport { public <T> T parse(Context context, TypedData body, TypeToken<T> type) { if (type.getRawType().equals(Integer.class)) { return Types.cast(Integer.valueOf(body.getText())); } else { return null; } } } public static class ExampleHandler implements Handler { public void handle(Context context) throws Exception { context.parse(Integer.class).then(integer -> context.render(integer.toString())); } } // unit test public static void main(String[] args) throws Exception { HandlingResult result = RequestFixture.handle(new ExampleHandler(), fixture -> fixture .body("10", "text/plain") .registry(registry -> registry.add(new IntParser())) ); assertEquals("10", result.rendered(String.class)); } }
-
-
Constructor Summary
Constructors Constructor Description NoOptParserSupport()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract <T> T
parse(Context context, TypedData requestBody, com.google.common.reflect.TypeToken<T> type)
The parser implementation.<T> T
parse(Context context, TypedData requestBody, Parse<T,Void> parse)
Delegates toparse(Context, TypedData, TypeToken)
, discarding the opts object of the givenparse
.-
Methods inherited from class ratpack.core.parse.ParserSupport
getOptsType
-
-
-
-
Method Detail
-
parse
public final <T> T parse(Context context, TypedData requestBody, Parse<T,Void> parse) throws Exception
Delegates toparse(Context, TypedData, TypeToken)
, discarding the opts object of the givenparse
.- Type Parameters:
T
- the type of object to construct from the request body- Parameters:
context
- The context to deserializerequestBody
- The request body to deserializeparse
- The description of how to parse the request body- Returns:
- the result of calling
parse(Context, TypedData, TypeToken)
- Throws:
Exception
- any exception thrown byparse(Context, TypedData, TypeToken)
-
parse
protected abstract <T> T parse(Context context, TypedData requestBody, com.google.common.reflect.TypeToken<T> type) throws Exception
The parser implementation.- Type Parameters:
T
- the type of object to construct from the request body- Parameters:
context
- The context to deserializerequestBody
- The request body to deserializetype
- the type of object to construct from the request body- Returns:
- an instance of
T
if this parser can construct this type, otherwisenull
- Throws:
Exception
- any exception thrown while parsing
-
-