search

Home  >  Q&A  >  body text

java - Design of classes in microservice architecture and restful return of json data

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?

扔个三星炸死你扔个三星炸死你2739 days ago823

reply all(2)I'll reply

  • 迷茫

    迷茫2017-06-23 09:15:35

    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.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-23 09:15:35

    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.

    reply
    0
  • Cancelreply