Как добиться проверки входных бинов с использованием джерси? - PullRequest
1 голос
/ 28 января 2020

Я пытаюсь выполнить простую валидацию входного бина, используя джерси, но я не могу получить какую-либо ошибку проверки, когда я делаю POST-запрос, используя почтальон, передавая ввод JSON. Во входных данных, хотя я и дал вес 9, проверки не происходит, хотя я дал @Min (значение = 10, сообщение = "Вес плода должен быть не менее 10") в Fruit. java.

Вот мой код ниже

Json Ввод

    {
        "weight":9,
        "colour":"red",
        "name":"rahul"
    }

ApplicationConfig. java

package com.ofs.fsapps.pft.applicationconfig;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;

@ApplicationPath("/abc")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        packages("com.main.app.*");
        property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
    }
}

Resource. java

@Path("v1/allochist")
public class AllocHistController {

    @POST
    @Path("/test")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createFruit(@Valid Fruit obj) {

        Fruit fruit = obj;
        System.out.println(fruit.getWeight());
        System.out.println(fruit.getName());
        System.out.println(fruit.getColour());
        return Response.ok(fruit, MediaType.APPLICATION_JSON).build();
    }

Fruit. java

package com.ofs.fsapps.pft.allochist.model;

import javax.validation.constraints.Min;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Fruit {
    @Min(value = 10, message = "Fruit weight must be 10 or greater")
    private Integer weight;
    private String name;
    private String colour;

    public Fruit() 
    {

    }

    public Fruit(Integer weight, String name, String colour) {
        this.weight = weight;
        this.name = name;
        this.colour = colour;
    }



    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }
}

build.gradle

apply plugin: 'war'


repositories {
    mavenCentral()
    flatDir {
       dirs 'libs'
   }
}

dependencies {  
    compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version:'   
    compile group: 'org.glassfish.jersey.inject', name: 'jersey-hk2', version:' 
    compile group: 'org.json', name: 'json', version:'  
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '   
    compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-json-provider', version: '   
    compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.6'
    compile group: 'javax.persistence', name: 'javax.persistence-api', version: '
    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '
    compile group: 'org.glassfish.jersey.ext', name: 'jersey-bean-validation', version: '    
    implementation name: 'ojdbc7-12.1.0.2'
    compile fileTree(dir: 'lib', includes: ['*.jar']) 
}
war {
   baseName = 'demo-context'
}

...