I'm trying to create a webservice that returns an int. It should also have an optional parameter - this fails.
This works:
package foo;
import java.util.Map;
import java.util.TreeMap;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/euler")
public class Euler1 {
@GET
@Path("/getmultiples")
@Produces(MediaType.APPLICATION_JSON)
public static Map<String, Integer> getMultiples() {
int sum = 0;
int limit = 1000;
for (int i = 0; i < limit; i += 3) {
sum += i;
}
for (int i = 0; i < limit; i += 5) {
if (i % 3 != 0) {
sum += i;
}
}
final Map<String, Integer> result = new TreeMap<>();
result.put("Sum", sum);
return result;
}
}
If I call it like curl http://localhost:8080/foo/rest/euler/getmultiples
I get {"Sum":233168}
.
But the limit should be an optional parameter. So I change the method signature to
public static Map<String, Integer> getMultiples(
@DefaultValue("1000") @QueryParam(value = "limit") int limit)
But then the previous returns nothing, and the server (Wildfly 8) logs
15:07:08,889 ERROR [io.undertow.request] (default task-3) UT005023: Exception handling request to /foo/rest/euler/getmultiples: java.lang.ArrayIndexOutOfBoundsException: 0
at org.jboss.resteasy.plugins.validation.GeneralValidatorImpl.parametersResolveToSameTypes(GeneralValidatorImpl.java:515) [resteasy-validator-provider-11-3.0.10.Final.jar:]
at org.jboss.resteasy.plugins.validation.GeneralValidatorImpl.overrides(GeneralValidatorImpl.java:486) [resteasy-validator-provider-11-3.0.10.Final.jar:]
at org.jboss.resteasy.plugins.validation.GeneralValidatorImpl.getSuperMethod(GeneralValidatorImpl.java:444) [resteasy-validator-provider-11-3.0.10.Final.jar:]
at org.jboss.resteasy.plugins.validation.GeneralValidatorImpl.getExecutableTypesOnMethodInHierarchy(GeneralValidatorImpl.java:335) [resteasy-validator-provider-11-3.0.10.Final.jar:]
at org.jboss.resteasy.plugins.validation.GeneralValidatorImpl.isMethodValidatable(GeneralValidatorImpl.java:265) [resteasy-validator-provider-11-3.0.10.Final.jar:]
Here's the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
The archetype used is the latest version of com.airhacks:javaee7-essentials-archetype
.
Here's the pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.oneiros</groupId>
<artifactId>webservice-ee7</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>webservice-ee7</finalName>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
</project>
How can I pass an optional parameter to my method?