Home  >  Q&A  >  body text

Interface Document - How does the java background interface conveniently return restful data?

When using spring mvc,
the json data you pass to the front end (ios/android) is converted by spring to the beans, right?
Are these beans all defined java classes?

Because I feel that the json required by the front end is very flexible. If the backend is encapsulated with several large and complete beans, the front end will get many useless fields.
(Although you can control the serialization strategy so that fields with null values ​​or even default values ​​do not appear in json, but in this way, the fields will be incomplete and the readability will be poor.)
If you refine the bean, you need Too many beans are created.

In our project, we basically use map to construct the required data (mainly because we use jdbctemplate, and the queried data is map type). There is no need to define bean classes. Although it is very flexible, it is often troublesome to write the key name by hand. location.

How did everyone solve it? Please feel free to give me your advice, thank you!

PHP中文网PHP中文网2674 days ago672

reply all(2)I'll reply

  • PHPz

    PHPz2017-05-27 17:41:59

    Use Spring WebMVC’s Controller directly and add @ResponseBody to return POJO

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-27 17:41:59

    In the past, Map was used to combine data, but handwriting Key names is really troublesome and easy to make mistakes. This is the so-called magic value and should be avoided as much as possible. Later, they switched to small and beautiful beans. Each interface corresponds to a bean. Although the number is relatively large, the coupling is looser than establishing a large and comprehensive bean. Modifying the bean will not affect other interfaces, which is still acceptable. I don’t know if there is one. There is no other more elegant solution.

    reply
    0
  • Cancelreply