Created
September 25, 2014 15:09
-
-
Save npatta01/41793b570fb696b8aba6 to your computer and use it in GitHub Desktop.
Validation of Json Model
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
if you post to | |
http://localhost:8080/{SERVICENAME}/rest/manual/recordChannel | |
{ | |
"deviceId" : "8880552999379356157" | |
} | |
You will get the below message with status code 400 | |
{accountId.invalid} (path = ManualResource.recordChannel.arg0.accountId, invalidValue = null) |
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
public class RecordChannel { | |
@NotNull(message = "{accountId.invalid}") | |
@Length(min = 2, max = 20) | |
public String accountId; | |
public String deviceId; | |
..... | |
} | |
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
@Path("manual") | |
public class ManualResource { | |
...... | |
@POST | |
@Produces(MediaType.APPLICATION_JSON) | |
@Consumes(MediaType.APPLICATION_JSON) | |
@Path("/recordChannel") | |
public ResourceResponse<String> recordChannel(@Valid RecordChannel recordChannel){ | |
} | |
...... | |
######The Valid tag validates that the annotation in Record channel is valid | |
} |
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
package com.comcast; | |
import com.comcast.resources.CatResource; | |
import com.comcast.resources.MyObjectMapperProvider; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.jackson.JacksonFeature; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.server.ServerProperties; | |
import org.glassfish.jersey.server.TracingConfig; | |
import org.glassfish.jersey.server.validation.ValidationConfig; | |
import org.glassfish.jersey.server.validation.internal.InjectingConstraintValidatorFactory; | |
import javax.validation.ParameterNameProvider; | |
import javax.validation.Validation; | |
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.container.ResourceContext; | |
import javax.ws.rs.core.Context; | |
import javax.ws.rs.ext.ContextResolver; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Method; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* Created by npatta001c on 9/18/2014. | |
*/ | |
@ApplicationPath("/rest") | |
public class MyApplication extends ResourceConfig { | |
public MyApplication(){ | |
packages(CatResource.class.getPackage().getName()); | |
// Validation. | |
register(ValidationConfigurationContextResolver.class); | |
.... | |
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE,true); | |
} | |
/** | |
* Custom configuration of validation. This configuration defines custom: | |
* <ul> | |
* <li>ConstraintValidationFactory - so that validators are able to inject Jersey providers/resources.</li> | |
* <li>ParameterNameProvider - if method input parameters are invalid, this class returns actual parameter names | |
* instead of the default ones ({@code arg0, arg1, ..})</li> | |
* </ul> | |
*/ | |
public static class ValidationConfigurationContextResolver implements ContextResolver<ValidationConfig> { | |
@Context | |
private ResourceContext resourceContext; | |
@Override | |
public ValidationConfig getContext(final Class<?> type) { | |
return new ValidationConfig() | |
.constraintValidatorFactory(resourceContext.getResource(InjectingConstraintValidatorFactory.class)) | |
.parameterNameProvider(new CustomParameterNameProvider()); | |
} | |
/** | |
* See ContactCardTest#testAddInvalidContact. | |
*/ | |
private class CustomParameterNameProvider implements ParameterNameProvider { | |
private final ParameterNameProvider nameProvider; | |
public CustomParameterNameProvider() { | |
nameProvider = Validation.byDefaultProvider().configure().getDefaultParameterNameProvider(); | |
} | |
@Override | |
public List<String> getParameterNames(final Constructor<?> constructor) { | |
return nameProvider.getParameterNames(constructor); | |
} | |
@Override | |
public List<String> getParameterNames(final Method method) { | |
// See ContactCardTest#testAddInvalidContact. | |
if ("addContact".equals(method.getName())) { | |
return Arrays.asList("contact"); | |
} | |
return nameProvider.getParameterNames(method); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment