2

I need to document multiple microservices api call,so I have a question that how to create json string out of java pojo class directly. I mean say for example ,

MyPojo.java

public class MyPojo {
String name;
List<String> address;
public MyPojo() {
    // TODO Auto-generated constructor stub
}
//setters and getters

}

now I need the string json structure of the pojo without creating object of the class.May be same the way swagger api creates json structure of @RequestBody object in web UI.

something like:

String jsonStruct=SomeUtil.convertPojoToJson(MyPojo.class)

then it should give like:

{"name":"string","address":[]}

My Try:

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.Schema;

public class TEst {
public static void main(String[] args) throws JsonProcessingException {

    ObjectMapper obj = new ObjectMapper(); 

    MyPojo o=new MyPojo();
    o.setName("aa");
    List<String> l=Arrays.asList("a","s");
    o.setAddress(l);
    System.out.println(obj.writeValueAsString(o));

}
}

actual o/p:

 {"name":"aa","address":["a","s"]}

required o/p:

 {"name":"string","address":["string"]}

CONCERN: But I need to create without creating object as in real the pojo is huge and not possible to set all dummy data.

5
  • 1
    you can't. in order to get that "a" and "s" in there, that have to be values in the object. a json represents a class, true, but the values in the json represent the values of the instance(s). Commented Nov 13, 2019 at 7:06
  • Maps, Sets/Lists would do the trick as well. I used them to create a dynamic model in json representation. You can still use the ObjectMapper. Give it a try and take a look, if the result is what you expect. Commented Nov 13, 2019 at 7:09
  • I updated the json. I require only structure not the values. Commented Nov 13, 2019 at 7:20
  • @ManuelPolacek I already tried with ObjectMapper but it gives like: {"name":null,"address":null}...address should be [] not null. Commented Nov 13, 2019 at 7:23
  • 2
    I think you can achieve this with the help of jackson-module-jsonSchema. Commented Nov 13, 2019 at 7:58

1 Answer 1

3

You could use Podam

PODAM is a lightweight tool to auto-fill Java POJOs with data. This comes handy when developing unit tests. Thanks to PODAM users now have a one-liner that does all the work them.

Add PODAM dependency in your project

<dependency>
  <groupId>uk.co.jemos.podam</groupId>
  <artifactId>podam</artifactId>
  <version>[latest.version]</version>
  <!-- <scope>test</scope> -->
</dependency>
  • Define your DataProviderStrategy if you don't want the default (Random data)
  • Define PodamFactory bean, initialized with the Data Provider Strategy
  • Use the PodamFactory bean in your code
 PodamFactory factory = new PodamFactoryImpl();
 MyPojo myPojo = factory.manufacturePojo(MyPojo .class);
 // write it as json
 System.out.println(new ObjectMapper().writeValueAsString(myPojo));
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you man it works as expected...only default data is horrible,proper DataProviderStrategy example not getting if you have please share. ACCEPTED!
I'm going to update my question with a DataProviderStrategy implementation to get this ` {"name":"string","address":["string"]}` ASAP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.