简体   繁体   中英

Convert Json Rest to Java DTO

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM