Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Custom validator enabling in Jetty

Thank you for replying, Oliver.

I created a stand-alone version of my validation attempt complete with .pom, etc.  It was developed in IntelliJ IDEA Community and should be Eclipse-compatible just fine.  You ought to be able to just grab it and run it.

https://github.com/mcc99/samples/tree/master/customer-validator

Problem: It works OK.  I.e., you hit it with: http://localhost:9086/validation/validator/US/USA and it *should* be returning an exception.  Instead it returns OK.  Only if the URL read something like http://localhost:9086/validation/validator/USA/USA would I expect it to return OK.

If anyone has trouble getting to it please let me know but it ought to be public just fine.

All interested, please eyeball my stuff please go ahead and supply feedback to me directly or on this list.  I am at mcc99@xxxxxxxxxxx.

TIA,

Matt




From: jetty-users-bounces@xxxxxxxxxxx <jetty-users-bounces@xxxxxxxxxxx> on behalf of Olivier Lamy <olamy@xxxxxxxxxxx>
Sent: Monday, October 15, 2018 1:38 AM
To: JETTY user mailing list
Subject: Re: [jetty-users] Custom validator enabling in Jetty
 
Hi
A bit complicated to help without context.
How do you package your app? Is there any web.xml?
Do you have any sample project in github?

On Sat, Oct 13, 2018 at 2:19 AM Matt Campbell <mcc99@xxxxxxxxxxx> wrote:
Please forgive me if this has been fielded before.  I can't imagine it hasn't.  I am trying to implement a custom validator in a REST services app using Jetty as the server.  The validator annotations despite dependencies being declared simply are not being initialized at all.  This is driving me crazy.  I have scoured the web for info about this but cannot find anything that addresses it.

My .pom file includes:

---
.
.
.
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.13.Final</version>
            <scope>provided</scope>
        </dependency>
.
.
.
---

Also under <plugins> I have:

---
.
.
.
<plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty-maven-plugin-version}</version>
                <configuration>
                    <jvmArgs>-Xmx2048m -Xms512m -XX:MaxPermSize=512m</jvmArgs>
                    <httpConnector>
                        <port>9086</port>
                    </httpConnector>
                    <stopPort>9968</stopPort>
                    <stopKey>jetty-stop</stopKey>
                    <stopWait>10</stopWait>
                    <webApp>
                        <contextPath>/customer</contextPath>
                    </webApp>
                    <useTestScope>true</useTestScope>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                        <version>2.0.1.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate.validator</groupId>
                        <artifactId>hibernate-validator</artifactId>
                        <version>6.0.13.Final</version>
                    </dependency>
                </dependencies>
            </plugin>
.
.
.
---

Just to try to get it working I have a service that takes two URL parameters and checks to see if they are the same.  If not, the validation fails.  The validator interface class:

----

package com.xxx.customer.validator;

import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.CONSTRUCTOR;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

@Target({METHOD, CONSTRUCTOR, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = Countries2ValidatorImpl.class)
@Documented
public @interface Countries2Validator {
    String message() default "The two country code values don't match";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

---

The validator impl:

---

package com.xxx.customer.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.constraintvalidation.SupportedValidationTarget;
import javax.validation.constraintvalidation.ValidationTarget;

@SupportedValidationTarget(ValidationTarget.PARAMETERS)
public class Countries2ValidatorImpl implements ConstraintValidator<Countries2Validator, Object[]> {

    @Override
    public void initialize(Countries2Validator countries2Validator) {
    }

    @Override
    public boolean isValid(Object[] values, ConstraintValidatorContext constraintValidatorContext) {
        String countryCode = (String) values[0];
        String anotherValue = (String) values[1];
        return countryCode.equals(anotherValue);
    }
}

---

Service interface class:

---

package com.xxx.customer.jaxrs.countries;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import com.xxx.api.address.v1.Countries;
import com.xxx.api.address.v1.Country;
import com.xxx.customer.validator.Countries2Validator;

public interface CountriesRS {
    @Path("/{countryCode}/{anotherValue}")
    @GET
    @Countries2Validator
    Response getCountryByCode2(@PathParam("countryCode") String countryCode, @PathParam("anotherValue") String anotherValue);
}

---

Srvc impl class:

---

package com.xxx.customer.jaxrs.countries;

import org.springframework.stereotype.Component;
import com.xxx.customer.validator.Countries2Validator;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Component
public class CountriesRSImpl implements CountriesRS {
    
    @Override
    @Path("/{countryCode}/{anotherValue}")
    @GET
    @Countries2Validator
    public Response getCountryByCode2(String countryCode, String anotherValue) {
        return Response.ok("OK").build();
    }
}

---

I am calling the service using Postman.  The URL is:  http://localhost:9086/customer/v1/countries/UaS/USA

I get a 200 response instead of an error.  I have added System.out()s to initialize() etc. and nothing is printed to console.  This is how I know the validator isn't even being initialized much less called.
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users


--
Olivier

Back to the top