Skip to content

Loading…

Updated the Request object to enable type capture on the attribute(String) method. #176

Merged
merged 1 commit into from

2 participants

@jkwatson

Improves the usability of the Request API, now that spark requires java 8.

@perwendel
Owner

I'm on vacation and I just have my phone so I can't test this. Should the method not change the parameter type to thw generic type. The current signature is strange since it just works for stri gs.

@jkwatson
@perwendel
Owner

deleted due to stupidity...

@perwendel
Owner

Aah, sorry, now I get it. Mushy brain.

@perwendel perwendel merged commit d82881f into perwendel:master

1 check passed

Details continuous-integration/travis-ci The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 19, 2014
  1. @jkwatson
View
4 src/main/java/spark/Request.java
@@ -301,8 +301,8 @@ public void attribute(String attribute, Object value) {
* @param attribute The attribute value or null if not present
* @return the value for the provided attribute
*/
- public Object attribute(String attribute) {
- return servletRequest.getAttribute(attribute);
+ public <T> T attribute(String attribute) {
+ return (T) servletRequest.getAttribute(attribute);
}
View
2 src/main/java/spark/webserver/RequestWrapper.java
@@ -178,7 +178,7 @@ public void attribute(String attribute, Object value) {
}
@Override
- public Object attribute(String attribute) {
+ public <T> T attribute(String attribute) {
return delegate.attribute(attribute);
}
View
4 src/test/java/spark/examples/filter/FilterExampleAttributes.java
@@ -44,12 +44,12 @@ public static void main(String[] args) {
});
after("/hi", (request, response) -> {
- Object foo = request.attribute("foo");
+ String foo = request.attribute("foo");
response.body(asXml("foo", foo));
});
}
- private static String asXml(String name, Object value) {
+ private static String asXml(String name, String value) {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><" + name + ">" + value + "</" + name + ">";
}
Something went wrong with that request. Please try again.