jsonschema2pojo
jsonschema2pojo copied to clipboard
How to create a simple pojo class with map attribute, not able to find any example.
I want to create a simple pojo class like:
public class VendorFields {
private String id;
private String vendorName;
private Map<String, CustomClass> vendorAttributes = new HashMap<String, vendorAttributes>(); //Not able to create this.
}
public class CustomClass{
String URL;
String endPoint;
}
the Json i am using:
{
"title": "Resource",
"description": "Resource Object",
"type": "object",
"properties": {
"id": {
"description": "id",
"type": "string"
},
"vendorName": {
"description": "Vendor Name",
"type": "string"
},
"vendorAttributes": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/CustomClass"
}
}
},
"definitions": {
"CustomClass": {
"properties": {
"URL": {
"description": "URL resource",
"type": "string"
},
"endPoint": {
"description": "URL resource",
"type": "string"
}
}
}
},
"required": [
"vendorName"
]
}
but getting 3 classes as output:
public class VendorFields {
private String id;
private String vendorName;
private VendorAttributes vendorAttributes; // this is not required;
private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>(); //this is also wrong.
}
public class VendorAttributes {
private Map<String, CustomClass> additionalProperties = new LinkedHashMap<String, CustomClass>();
}
public class CustomClass {
private String url;
private String endPoint;
}
What property to set or this is the expected behavior?
Hi. When posting code snippets like this, please use backtick blocks like this:
https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#syntax-highlighting
It makes things a lot easier to read. I have added them on your example.
Could you try adding this inside the vendorAttributes schema:
"existingJavaType":"java.util.Map<String, CustomClass>",
You will probably have to pull CustomClass out into its own file so that this gets generated too.
I wanted just a map of List values like below: I just wanted a field to be private Map<String, List<String>> varieties
"varieties" : {
"type" : "object",
"existingJavaType" : "java.util.Map<String,List<String>>",
"additionalProperties" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
But I see that the generated code fails compilation because it doesn't recognize java.util.List
and doesn't import in the generated code :( Any help please @joelittlejohn ? Thanks !
Hi
Please use fully qualified names when referring to types that are outside same or java.lang
package, eg.:
"varieties": {
"type": "object",
"existingJavaType": "java.util.Map<String, java.util.List<String>>",
"additionalProperties": {
"type": "array",
"items": {
"type": "string"
}
}
}