I am writing an annotation processor and i need to scan all classes with certain annotation to get all fields and create json object with same structure of the class.
For example:
#ClassToJson
public class Person {
private String name;
private String surname;
/*getter and setter*/
}
My output is:
{
"name": "string",
"surname": "string"
}
Now i am wondering how can i handle classes like this one:
public class PhoneNumber {
private String countryCode;
private String phoneNumber;
/*getter and setter*/
}
#ClassToJson
public class Person {
private String name;
private String surname;
private PhoneNumber phoneNumber;
/*getter and setter*/
}
i would like get output like this:
{
"name": "string",
"surname": "string",
"phoneNumber": {
"countryCode": "string",
"phoneNumber": "string"
}
}
Related
POST feeds/_doc
{
"feed_id": "3",
"title": "jose ",
"body": "This is Jose allen",
"comment": [
{
"c_id": "13",
"feed_id": "2",
"c_text": "hey"
},
{
"c_id": "13",
"feed_id": "3",
"c_text": " here"
}
]
}
this is the mapping
PUT feeds
{
"mappings": {
"properties": {
"comment":{
"type": "nested"
}
}
}
}
I want to create feed class with mentioned feilds the comment field has nested type fields with sub fileds of c_text,c_id .how can i create it
#Document(indexName = "feeds")
public class Feed {
#Id
private String feed_id;
#Field(type = FieldType.Text, name = "title")
private String title;
#Field(type = FieldType.Text, name = "body")
private String body;
#Field(type= FieldType.Nested , name="comment")
private String[] comment;
}
how to intialize subfields of comment field as i am using elasticsearch 7.17.3
You can create a Comment class like this:
public class Comment {
#JsonProperty("c_id")
private String cId;
#JsonProperty("feed_id")
private String feedId;
#JsonProperty("c_text")
private String cText;
// Getters, setters, ...
}
and instead of using private String[] comment, you should use:
#Field(type= FieldType.Nested , name="comment")
private List<Comment> comment;
I'm learning how to properly use DTOs and so far everything was going well until I got this problem:
I have a Model called Student:
#Data
#Document
#Builder
public class Student {
#Id
private String id;
#Indexed(unique = true)
private Name name;
#Indexed(unique = true)
private String email;
private Gender gender;
private Address address;
private List<String> favoriteSubjects;
private BigDecimal totalSpentInBooks;
private LocalDateTime created;
}
Than I create a DTO to fetch some data:
#Data
public class StudentDto {
private String firstName;
private String lastName;
private String email;
private String country;
}
Here is the response using the DTO from Postman:
{
"firstName": "Gabriel",
"lastName": "Vendramini",
"email": "gabriel.vendramini#email.com",
"country": null
}
Here is the same response but with no DTO:
{
"id": "61f7d87bb4534b452993b02e",
"name": {
"firstName": "Gabriel",
"lastName": "Vendramini"
},
"email": "gabriel.vendramini#email.com",
"gender": "MALE",
"address": {
"country": "Brazil",
"city": "Sao Paulo",
"postCode": "08006"
},
"favoriteSubjects": [
"Computer Science",
"Mathematic",
"Physic"
],
"totalSpentInBooks": 10,
"created": "2022-01-31T13:39:23.023"
}
Here are the Controller:
#GetMapping("/get-all")
public ResponseEntity<List<Student>> fetchAllStudents() {
log.info("Get All Students - Controller Call");
return ResponseEntity.ok(studentService.getAllStudents());
}
#GetMapping("/get-all-dto")
public ResponseEntity<List<StudentDto>> fetchAllStudentsDto() {
log.info("Get All Students With DTO - Controller Call");
return ResponseEntity.ok(studentService.getAllStudents().stream().map(studen
t -> modelMapper.map(student,
StudentDto.class)).collect(Collectors.toList()));
}
Here are the Models for Address and Name:
#Data
#AllArgsConstructor
#Builder
public class Address {
private String country;
private String city;
private String postCode;
}
#Data
#AllArgsConstructor
#Builder
public class Name {
private String firstName;
private String lastName;
}
The structure is the same for both. I tried to pass all the attributes from Address, change the order.
How can I use MapStruct to create a mapper that maps a list (my source) to a object with a list (destination)?
My source classes looks like this:
class SourceB {
private String name;
private String lastname;
}
class SourceA {
private Integer id;
private List<SourceB> bs;
}
so I need to transform it to this:
class DestinationA {
private Integer id;
private DestinationAB bs;
}
class DestinationAB {
private List<DestinationB> b;
}
class DestinationB {
private String name;
private String lastname;
}
Expected sample json:
source:
{
"id": 1,
"bs": [
{
"name": "name1",
"lastname": "last1"
},
{
"name": "name2",
"lastname": "last2"
}
]
}
destination:
{
"id": 1,
"bs": {
"b": [
{
"name": "name1",
"lastname": "last1"
},
{
"name": "name2",
"lastname": "last2"
}
]
}
}
It's quite simple. Just put #Mapping annotation with specified source and destination on top of the mapping method.
#Mapper
public interface SourceMapper {
#Mapping(source = "bs", target = "bs.b")
DestinationA sourceAToDestinationA(SourceA sourceA);
}
I have a class called Customer.
#Entity
#Table(name = "customer")
public class Customer {
#Id
#Column(unique = true)
private String userId;
#Column(unique = true)
private String userName;
private String fullName;
#Column(unique = true)
private String emailAddress;
private String password;
private String country;
#ElementCollection
private Collection<ContactNum> contactNums = new ArrayList<>();
private String district;
private String dateOfBirth;
private String gender;
}
and there is a collection of contact numbers.
#XmlRootElement
#Embeddable
public class ContactNum {
private String landLine;
#Column(unique = true)
private String mobile;
public String getLandLine() {
return landLine;
}
public void setLandLine(String landLine) {
this.landLine = landLine;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
My REST API is getting a POST Request JSON Object which is Customer and Contact number inside it.
{
"userName": "aaaa",
"fullName": "aaaa",
"emailAddress": "aaaa",
"password": "aaaa",
"country": "aaaa",
"contactNums" : {
"landLine": "0000000000",
"mobile": "0000000000"
},
"district": "aaaa",
"dateOfBirth": "813695400000",
"gender": "aaaa"
}
How can I map that request in my JAX-RS client? My method to get request is this. And I also use Hibernate as an ORM tool.
#POST
#Path("registerCustomer")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response registerCustomer(Customer newCustomer) {
}
If you're using Jackson to handle JSON conversion.
You can either use a custom deserializer (via #JsonDeserialize
on class level).
Or write an adapter which converts your ContactNum
to a Collection of ContactNum's.
But if you change your JSON input from
"contactNums" : {
"landLine": "0000000000",
"mobile": "0000000000"
}
to
"contactNums" : [{
"landLine": "0000000000",
"mobile": "0000000000"
}]
(contactNums changed from object to array of objects)
This way the conversion should work out of the box.
I have a Json list using object with children
{
"id":"154",
"name":"peter",
"children": [
{
"id":"122",
"name": "mick",
"children":[]
},
{
"id":"123",
"name": "mick",
"children":[]
}
]
}
Here is the class of my object:
public class person{
private String id;
private String name;
private List<person> children;
//getters and setters
}
When I try to deserialize this object, I have the following error
Can not deserialize instance of person out of START_ARRAY token
What should I do ?
The JSON contains an array of persons.
Your class a List of person.
Either change the JSON like #Naveed Yadav suggested or change the class to
public class Person{
private String id;
private String name;
private Person[] children;
//getters and setters
}
(BTW the class name should be upper case in Java)
Fix syntax errors in your JSON body and you'll be in a good shape:
{
"id":"154",
"name":"peter",
"children": [
{
"id":"122",
"name": "mick",
"children":[], <== Excess comma
} <== Missing comma
{
"id":"123",
"name": "mick",
"children":[], <== Excess comma
}
]
}
Valid one:
{
"id": "154",
"name": "peter",
"children": [{
"id": "122",
"name": "mick",
"children": []
},
{
"id": "123",
"name": "mick",
"children": []
}
]
}
You need to change your POJO declaration like below:-
public class person{
private String id;
private String name;
private List<Children> children;
//getters and setters
private class Children{
private String id;
private String name;
private String[] children;
}
{
"id":"154",
"name":"peter",
"children":
{
"id":"122",
"name": "mick",
"children":[],
}
{
"id":"123",
"name": "mick",
"children":[],
}
}