我正在尝试使用jersey做一个简单的Input Bean验证,但是当我通过传递输入JSON使用邮递员发出POST请求时,我无法获得任何验证错误。 在输入中,尽管我给出的权重为9,但在Fruit.java中给出了@Min(值= 10,消息=“水果的权重必须等于或大于10”),但是没有进行验证。
下面是我的代码
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'
}