-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieve bean validation parameter names from Spring MVC / JAX-RS ann…
- Loading branch information
Showing
17 changed files
with
410 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...java-ee/src/main/java/io/github/belgif/rest/problem/jaxrs/JaxRsParameterNameProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.github.belgif.rest.problem.jaxrs; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Executable; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.validation.ParameterNameProvider; | ||
import javax.ws.rs.CookieParam; | ||
import javax.ws.rs.FormParam; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.MatrixParam; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
/** | ||
* A ParameterNameProvider that retrieves the parameter name from JAX-RS annotations (if present). | ||
* | ||
* @see ParameterNameProvider | ||
*/ | ||
@Provider | ||
public class JaxRsParameterNameProvider implements ParameterNameProvider { | ||
|
||
private static final ConcurrentHashMap<Executable, List<String>> PARAMETER_NAME_CACHE = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public List<String> getParameterNames(Constructor<?> constructor) { | ||
return PARAMETER_NAME_CACHE.computeIfAbsent(constructor, this::getParameterNames); | ||
} | ||
|
||
@Override | ||
public List<String> getParameterNames(Method method) { | ||
return PARAMETER_NAME_CACHE.computeIfAbsent(method, this::getParameterNames); | ||
} | ||
|
||
private List<String> getParameterNames(Executable executable) { | ||
return Arrays.stream(executable.getParameters()) | ||
.map(this::getParameterName) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private String getParameterName(Parameter parameter) { | ||
Annotation[] annotations = parameter.getAnnotations(); | ||
for (Annotation annotation : annotations) { | ||
if (annotation instanceof QueryParam) { | ||
return ((QueryParam) annotation).value(); | ||
} else if (annotation instanceof PathParam) { | ||
return ((PathParam) annotation).value(); | ||
} else if (annotation instanceof HeaderParam) { | ||
return ((HeaderParam) annotation).value(); | ||
} else if (annotation instanceof CookieParam) { | ||
return ((CookieParam) annotation).value(); | ||
} else if (annotation instanceof FormParam) { | ||
return ((FormParam) annotation).value(); | ||
} else if (annotation instanceof MatrixParam) { | ||
return ((MatrixParam) annotation).value(); | ||
} | ||
} | ||
return parameter.getName(); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
belgif-rest-problem-java-ee/src/main/resources/META-INF/validation.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<validation-config xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration http://xmlns.jcp.org/xml/ns/validation/configuration/validation-configuration-2.0.xsd" | ||
version="2.0"> | ||
|
||
<parameter-name-provider>io.github.belgif.rest.problem.jaxrs.JaxRsParameterNameProvider</parameter-name-provider> | ||
|
||
</validation-config> |
Oops, something went wrong.