Closed ravibarkhani closed 1 year ago
Can you please edit your comment to fix the formatting? I can't really tell what you're asking here I'm afraid.
Hi
I suppose that <includeJsr303Annotations>true</includeJsr303Annotations>
might be missing in your maven configuration.
Given
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>jsonschema2pojo</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<includeJsr303Annotations>true</includeJsr303Annotations>
<!-- Mandatory parameter -->
<sourceDirectory>${basedir}/src/main/resources</sourceDirectory>
<!-- Optional configuration to reduce amount of generated code -->
<includeHashcodeAndEquals>false</includeHashcodeAndEquals>
<includeToString>false</includeToString>
<includeAdditionalProperties>false</includeAdditionalProperties>
</configuration>
</plugin>
And test.json
:
{
"type": "object",
"properties": {
"producer": {
"type": "string",
"pattern": "^[A-Z]{3}$"
}
}
}
Generates:
package json;
import javax.annotation.processing.Generated;
import javax.validation.constraints.Pattern;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"producer"
})
@Generated("jsonschema2pojo")
public class Test {
@JsonProperty("producer")
@Pattern(regexp = "^[A-Z]{3}$")
private String producer;
@JsonProperty("producer")
public String getProducer() {
return producer;
}
@JsonProperty("producer")
public void setProducer(String producer) {
this.producer = producer;
}
}
To get similar result on https://www.jsonschema2pojo.org you'd have to select either javax.validation
or jakarta.validation
in Validation annotations:
section
Have verified this works
Dear team, I am experiencing issue while generating RegEx pattern from maven plugin, Can you please suggest solution.
I would like to add RegEx pattern on String property but it is not generating required pattern using maven plugin. Same If I generate from website "https://www.jsonschema2pojo.org/" with
"type": "string", "pattern" : "^[A-Z]{3}$"
and select Validation annotations:javax.validation
It generates required RegEx on field.With below mentioned schema example code
{ "type": "object", "properties": { "producer": { "type": "string", "pattern" : "^[A-Z]{3}$" } } }
It generates Java class with plain String ()@JsonProperty("producer") private String producer; @JsonIgnore
{ "type": "object", "properties": { "producer": { "type": "string", "format" : "regex", "pattern" : "^[A-Z]{3}$" } } }
It generates Java class with plain String ()@JsonProperty("producer") private Pattern producer;
Maven dependencies : `
`