I'm trying to de-serialize class from JSON string using library com.alibaba.fastjson. This is the class:
#Getter
#ToString
#Setter
#JSONType(naming = PropertyNamingStrategy.CamelCase)
public class SiteInfo {
private String id;
...
#JSONField(name = "keywords")
private String keyWords;
...
}
After I run the code, the value of keyWords=null; In other hand, if I changing it to "private String keywords;" everything works fine. So I can't use the camelcase (according to java conventions).
In need some thing similar like #JsonProperty("keywords") from Jackson library, but for the alibaba fast json.
Related
I'm running into an issue with my pojo created using lombok with jsonproperty annotation. It doesn't respect the json annotation. And, when i create an object using the lombok builder it uses the field names on the object instead of json property.
Could someone help see what am I missing here. I just started using lombok so im hoping something straightforward. I'm running the code on Intellij
#Data
#Builder
public class pojo {
#JsonProperty("grant_type")
private final String grantType = "xyz";
#JsonProperty("client_id")
private String clientId;
}
It's default behavior of #Builder.
If we want the builder with setClientId, We can add setterPrefix = "set" into #Builder.
#Data
#Builder(setterPrefix = "set")
public class pojo {
#JsonProperty("grant_type")
private final String grantType = "xyz";
#JsonProperty("client_id")
private String clientId;
}
#Data would generate a pair of setter/getter. But setter is a member method of pojo, not pojoBuilder's.
More details of Builder is here.
I'm trying to create a Document with an #Id property different from String, everything works great. My issue is the way the document is stored in the Bucket, unfortunately it prefixes with the Class name:
ActorKey(key=d7471027-4bd1-40a3-8e29-5249f45beed4)
{
"name": "Emma Watson",
"_class": "com.guilherme.miguel.domain.Actor"
}
Is there a way to store the id like if it was a simple String?
This is to avoid issues in plain queries (simple queries will force me to use ActorKey(key=xxxxxxxxxxx))
Actor.java:
#Data
#AllArgsConstructor
#NoArgsConstructor
#Document
public class Actor {
#Id
private ActorKey key;
private String name;
}
ActorKey.java:
#Data
#AllArgsConstructor
#NoArgsConstructor
public class ActorKey implements Serializable {
private String key;
}
ActorRepository.java
#N1qlPrimaryIndexed
#ViewIndexed(designDoc = "actor")
public interface ActorRepository extends CouchbaseRepository<Actor, ActorKey> {
}
The underlying Couchbase SDK uses String as a Document identifier, so spring-data-couchbase will call toString() on your key objects. What you see is the result of the Lombok-generated toString() method (a generation that is part of the #Data annotation).
Simply explicitly override toString() in ActorKey!
I need to take XML that looks like something like the following:
<root:ElementName>
<equipment:Equipment>
<eqp:Name>Equipment 1</eqp:Name>
<eqp:Type>A</eqp:Type>
</equipment:Equipment>
<equipment:Equipment>
<eqp:Name>Equipment 2</eqp:Name>
<eqp:Type>B</eqp:Type>
</equipment:Equipment><equipment:Equipment>
<eqp:Name>Equipment 3</eqp:Name>
<eqp:Type>C</eqp:Type>
</equipment:Equipment>
</root:ElementName>
And I want to map that into a list of "Equipment" POJOs. I'm using Jackson XML mapping and Lombok, so basically I've got this split into two classes right now, first the root object which should read in that <root:ElementName> and turn all the <equipment:Equipment> tags into a list of equipment objects:
#JacksonXmlRootElement(localName = "root:ElementName")
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class EquipmentMidbCompositeResponse
{
#JsonProperty("equipment")
#JacksonXmlProperty(localName = "equipment:Equipment")
#Getter
#Setter
List<Equipment> equipmentList;
}
And then the Equipment object itself:
#Entity
#Data
#NoArgsConstructor
#EqualsAndHashCode(callSuper = true)
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public final class Equipment
{
#JsonCreator
public Equipment(String name){
}
#JsonProperty("EquipmentName")
#JacksonXmlProperty(localName = "eqp:Name")
#Setter
#Getter
private String name;
#JsonProperty("EquipmentType")
#JacksonXmlProperty(localName = "eqp:Type")
#Setter
#Getter
private String type;
}
At first I didn't have that constructor with #JsonCreator in the Equipment object and would get a "no String-argument constructor/factory method to deserialize from String value" error, and after some research added the constructor to get around that. With that I get past that error, but the list of Equipment objects that gets returned after mapping have all their fields set to null. What am I missing/doing wrong here when trying to map these XML properties?
(Posted on behalf of the OP).
I figured out the issue, my approach with the #JsonCreator was the incorrect way to go for that error. Turns out all I had to do was an a #JacksonXmlElementWrapper(useWrapping=false) annotation to my list item and everything went through just fine.
I have a web service client that needs to send json data in an HTTP POST. I have a need to provide an empty json object in one of the fields. I cannot omit the field, it must be an object, and I should not supply any fields inside this object because that would change the result. Only an empty object will do.
Can this be done in jackson solely using annotations? If there is any serialization or mapping configuration, I need that to apply only to this class. I'm hoping for a magic option to JsonInclude or JsonSerialize.
Desired serialization output:
{
"field1": "value1",
"field2": "value2",
"field3": {}
}
This is pretty close to my Java class:
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class BeanClass implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("field1")
private String field1;
#JsonProperty("field2")
private String field2;
#JsonProperty("field3")
private EmptyBean field3;
}
And the EmptyBean class pretty much looks like this:
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class EmptyBean implements Serializable {
private static final long serialVersionUID = 1L;
}
A way to turn off the FAIL_ON_EMPTY_BEAN serialization option with an annotation would get this done for me. This answer looks promising but focuses on configuration which looks like it would apply to my whole application (and I don't want that).
I am hoping to solve this solely through annotations if possible. But as long as I have a way to change the mapping only for this class, I'll be happy.
is there a way that I can take the DTO's from a REST api? I want to create my DTO's automaticaly from the JSON REST api. Is there some way?
You can try use a framework library like RESTEasy (Jboss Suite) or Jersey or Gson
Then you only need define a estructure same a you class for example, if your class is something like:
#Entity
#Table(name = "\"entityName\"")
public class Entity implements Serializable {
private static final long serialVersionUID = 3469107762875646075L;
#Id
private Integer id;
#Column
private String name;
public Entity() {
}
//getters and setters
The interface will receive an object of that type.
#POST
#Path("/create")
#Produces(MediaType.APPLICATION_JSON)
Response createEntity(Entity entityObject);
And JSON be this way, then the conversion is automatic.
{
"id":"99",
"name":"stackoverflow"
}
NOTE: The information received must be of the same type defined in your Class to perform this conversion.
After some years, this is what I wanted:
https://app.quicktype.io/