java - Design of classes in microservice architecture and restful return of json data
扔个三星炸死你
扔个三星炸死你 2017-06-23 09:13:35
0
2
816

Question 1: In the microservice architecture implemented by spring boot, data interaction issues between various services
Explanation

比如在服务架构中两个服务之间的交互,服务A中有一个数据model,
服务B调用后,服务A 将此类型的模型返回给服务B ,
服务B中就必须也定义一个这样的数据模型才能正确的接收数据这样会造成类的多次定义?  

Question 2: When using restful in spring to return client data, the model will be converted to json, but there are many attributes defined in the object, and the client only needs the values ​​corresponding to some of the attributes, so how to Remove unnecessary or valueless attributes before returning to the client?

扔个三星炸死你
扔个三星炸死你

reply all(2)
迷茫

Answer 1:
The commonly used data transmission formats for RPC communication are json, xml or custom binary formats, which are usually agreed in advance.
Since they are different services, they are generally maintained by different teams. Besides, the class of the parsed receiving object does not have to be completely consistent with the output object.

Answer 2:
One way is to define the necessary attributes of the output object, convert your intermediate object into the final object to be output, and remove redundant attributes.
Or remove redundant attributes when converting the object to json. Spring provides the @JsonIgnore annotation. After adding this annotation to the redundant attributes of the bean, the attribute will be ignored when converting the json object.

我想大声告诉你

Q1: Beans can be defined in an independent module, and both service A and service B depend on this module.

Q2: Ignore the json annotation or use dto to get only the required attributes.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template