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/
Related
Problem
Working with anemic domain objects with multiple foreign keys stored as Long's. Trying to protect against transposing the values with some kind of strong typed domain types.
For example given the following product review class:
public class Review {
private Long id;
private Long productId;
private int rating;
private String title;
private String body;
private Long createdById;
private Date createdAt;
//...
}
I'd like to prevent myself from accidentally transposing the wrong foreign key to any of the Long's:
ReviewDto dto = // some DTO that was parsed from JSON for example
Review review = new Review();
review.productId = dto.getCreatedById(); // see transpose error here as typical type checking doesn't catch it
Solution(s)
Inheritance
An obvious solution is to implement a simple class hierarchy for domain types.
public class LongId {
private Long id;
//...
}
//...
public class ReviewId extends LongId {...}
public class ProductId extends LongId {...}
//...
public class Review {
private ReviewId id;
private ProductId productId;
//...
}
//...
ReviewDto dto = // some DTO that was parsed from JSON for example
Review review = new Review();
review.productId = dto.getCreatedById(); // compile error as types don't match
The downside to this solution is that the actual type is contained and therefore laborious to marshal it to/from JSON and in/out of the database requiring me to write lots of custom serializers.
Generics
Another solution I've seen done is using generics but adds verbose syntax and still having to write custom serializers just to get at a simple type.
public class LongId<T> {
private Long id;
//...
}
//...
public interface ReviewId {}
public interface ProductId {}
//...
public class Review {
private LongId<ReviewId> id;
private LongId<ProductId> productId;
//...
}
//...
ReviewDto dto = // some DTO that was parsed from JSON for example
Review review = new Review();
review.productId = dto.getCreatedById(); // compile error as types don't match
Annotations?
Anyone pull this off with Java annotations? What was involved? The documentation landscape for Java annotations is sparse once you get past the hello world examples. What I have found gave only a passing reference that the system is pluggable and that I'd have to write my own maven plug-in to do the type checking. I'm very interested in this as a possible solution as I don't expect to have to write custom serializers among other boilerplate as the types are just plain Java reference types that are greatly supported by most JSON and database libraries.
#Documented
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.FIELD, ElementType.LOCAL_VARIABLE, ElementType.PARAMETER })
#TypeQualifier(applicableTo = Long.class)
public #interface ReviewId {}
//...
public class Review {
private #ReviewId Long id;
private #ProductId Long productId;
//...
}
//...
ReviewDto dto = // some DTO that was parsed from JSON for example
Review review = new Review();
review.productId = dto.getCreatedById(); // **magic** happens here so that both maven and IDE catch this at compile time
The Checker Framework is an annotation-based approach, as you requested.
The Checker Framework permits you to specify domain properties with type annotations, and it enforces those properties at compile time.
It is used at companies such as Amazon, Google, Uber, and many others.
You can use a pre-built type system, or you can build your own which can be as little as 4 lines of code (example). You do not need to write a Maven plug-in.
For more information, see the Checker Framework manual.
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.
I am in the process of rewriting a very old java app to Spring Boot and Hibernate 5. Part of this task requires that I replace our XML configuration (both Spring and Hibernate) with annotations.
I have the following question. Let's assume that the application contains a class as such:
public class MyObject implements Serializable {
private static final long serialVersionUID = 81848571841847187L;
private String id;
private String name;
//getters and setters...
}
This class Serialized across a network, and is included in a "common" jar, which classers must include, in order to deserialize on their end.
Let's assume that I add a few Hibernate and JPA annotations to the class
#Table(...)
#Entity
public class MyObject implements Serializable {
private static final long serialVersionUID = 81848571841847187L;
#Id
#Column(...)
private String id;
#Column(...)
private String name;
//getters and setters...
}
My question is: if the caller (who deserializes the above Object) does not have those annotations in his classpath, will serialization fail?
Only Annotations with RETENTION=RUNTIME used in byte code, but Serialization works with object fields, not with classes.
but its important to understand that Annotations can be used by custom serializer.
for example this is how #Transient exclusion is implemented.
so the next thing is to check what type of Serialization mechanism is used.
elad
I'm developing a Spring MVC REST application. I made a few simple controller like this:
#Controller
#RequestMapping("/agents")
public class AgentsController {
#Autowired
AgentsRepository agentsRepository;
#RequestMapping(value="/{id}",method=RequestMethod.GET)
public #ResponseBody Agents getAgents(#PathVariable Long id){
Agents agents = agenteRepository.findOne(id);
return agent;
}
#RequestMapping(method=RequestMethod.GET)
public #ResponseBody List<Agents> getAllAgents(){
return agentsRepository.findAll();
}
}
The agent class is annotated in this way:
#XmlRootElement
#Entity
public class Agents implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long idAgents;
private String name;
private String surname;
Now the problem is the following, when I try to do a http get with (host+post/myapplication/agents/1) everything work and I get inside my browser the xml structure, instead when I do (host+post/myapplication/agents) I don't get all agent list in a xml structure, but I get the toString of the collection.
What am I doing wrong?
I would like a plain forward DTO generation tool that would either
Generate it on the fly (e.g. cglib - create the class and DTO object on the fly)
Or an Eclipse plugin that will take the Entity and generate a DTO (user will specify which tree graph to include, and for non included, will include foreign keys instead of related entities etc)
E.g. take something like this
#Entity
#Table(name="my_entity")
public class MyEntity {
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
#ManyToOne
private RelatedEntity related;
public RelatedEntity getRelated(){
return related;
}
...
And generate something like this :
#Entity
#Table(name="my_entity")
public class MyEntity imlpements MyEntityDTO {
#Id #GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
#ManyToOne
private RelatedEntity related;
//overrides MyEntity interface, it's allowed to narrow return type
public RelatedEntity getRelated(){
return related;
}
...
//implements MYEntityDTO respective interfaces
public Long getRelatedId(){return related.getId();}
And DTO interface(s):
public interface MyEntityDTO {
public String getId();
public String getName();
public Long getRelatedId();
public RelatedEntityDTO getRelated(); //RelatedEntity implements RelatedEntityDTO
...
}
public interface RelatedEntityDTO {
...
}
If we don't want to include children in the graph, remove it from the DTO interface:
public interface MyEntityDTO {
public String getId();
public String getName();
public Long getRelatedId();
...
I'm sure there is some eclipse plugn for it and if not, I challange someone to write one, or explain why what I want is not helpful (and provide an alternative suggestion)
Probably Hibernate Tools should be doing this: http://hibernate.org/subprojects/tools.html
Telosys Tools can generate both : JPA entity and DTO
Let's have a look at this tutorial https://sites.google.com/site/telosystutorial/springmvc-jpa-springdatajpa
it generates a full Spring MVC CRUD application with JPA
Architecture : https://sites.google.com/site/telosystutorial/springmvc-jpa-springdatajpa/presentation/architecture
The mapper Entity/DTO is also generated (it uses "org.modelmapper" )
The templates are customizable
Try to look at:
https://github.com/nikelin/spring-data-generation-kit
But it's only suitable for you if your project is under the
Maven control.