Last time I wrote about layering and planning code for real projects, I mentioned a few things to note at the end of the article, the first of which was:

  • Contorller layer parameter passing is recommended to use data model definitions instead of HashMaps

In the private letter, many friends asked, why can’t we do this?

I thought to myself: Haven’t any of my friends been punched by their colleagues for doing this? (funny)

Ok, let’s talk about that today, because it’s a problem that’s often overlooked in actual code

Has anyone written that before?

I once took over an old project left by my predecessors. When I got the code and introduced IDEA, I cried.

Because its Controller layer code is written like this:

@restController@requestMapping ("/index") public class IndexController@postMapping ("/getIndexContent") public ResponseWrapper getIndexContent( @RequestBody Map<String, Object> paramMap ) { ResponseWrapper res = new ResponseWrapper(); // If (! paramMap.containsKey("article_id")) { res.setCode(500); Res.setmsg (" missing article_id information "); return res; } if (! paramMap.containsKey("page")) { res.setCode(500); Res.setmsg (" Missing page information "); return res; } if (! paramMap.containsKey("size")) { res.setCode(500); Res.setmsg (" missing size information "); return res; } if (! paramMap.containsKey("version")) { res.setCode(500); Res.setmsg (" Missing version information "); return res; } / /... } //...... is omitted } copy code is omitted hereCopy the code

What else can you do to pass a Map to a Controller method? ! It’s insane. Fortunately, there is also a validation of the validity of the passed parameters, for the passed parameters, I can at least guess, otherwise it is really mew.

Why not use Map when passing parameters to the Controller layer?

Map is good for a while, but good for maintenance

As it happens, this place has a living example of our friend.

I remember a friend asked me a question like this, saying that he had taken over an old project of others, and asked me a question like this:

See!

The first (and biggest) drawback to using Map as a parameter is that it causes the person who takes over and maintains it to doubt his life because he has no idea what parameters the code is passing, and the only way to construct parameters to debug the interface is by imagination, guesswork, and guesswork.

Just think about it. We can pass parameters anywhere in our code using Map. If we do that, we don’t need to define any data model classes.

The project he is working on uses the LinkedHashMap parameter, which is pretty cool.

On top of that, the question follows.

Good API tools are out of your reach

I wrote an article about the separation of the front and back ends, time for a good API management system! , talked about some of the more useful API management tools on the market now, can greatly improve the efficiency of front-end and back-end development, which is a great Gospel for front-end and back-end development.

Let’s use the Swagger API tool as an example, if the Controller pass parameter uses Map:

@apiOperation (" getIndexContent") @postMapping ("/getIndexContent") public ResponseWrapper getIndexContent( @RequestBody Map<String, Object> paramMap ) { // ...... } copy code is omitted hereCopy the code

The API tool cannot read the specific parameter items and parameter types, so it does not tell what the parameters are:

In other words, if I changed the Map pass parameter to the custom data model IndexQueryDto pass parameter:

PostMapping("/getIndexContent") public ResponseWrapper getIndexContent("/getIndexContent") @RequestBody IndexQueryDto indexQueryDto ) { // ...... @apiModel (value = "ApiModel ") class IndexQueryDto {@apiModelProperty (value =" article ID ") @notnull (message = "article_id missing ") private Long article_id; @APIModelProperty (value = "page number ") @notnull (message =" missing page information ") private Integer PAGE; @APIModelProperty (value = "number of items per page ") @notnull (message =" missing size information ") private Integer SIZE; @APIModelProperty (value = "App version ") @notnull (message = "missing version information ") private String Version; / /... The set/get method} copy code is omitted hereCopy the code

API tools like Swagger are handy for managing parameters:

In this way, both debugging themselves, and the front and back end interfaces will be much more convenient.

Similarly, in addition to Swagger, RestfulToolkit, a very useful interface management plug-in recommended in my previous article, does not recognize the specific parameters of the Map type:

However, for the definition parameters of the data model, the details of the parameters can be very clear, and the interface test can be easily provided:

Good annotations are no longer usable

If () = if(); if() = if(); if() = if();

But the question is, do we really need this kind of eye-stinging manual serial if() judgment for parameter verification?

Also in the previous post, “What? I hear you still write complex parameter verification?” As mentioned, we can use annotations to easily circumvent verbose parameter validation, but only if we can’t use Map to pass parameters and use data model definitions, like this:

Class IndexQueryDto {@notnull (message = "article_id info ") private Long article_id; @notnull (message = "missing page information ") private Integer Page; @notnull (message = "missing size message ") private Integer size; @notnull (message = "missing version information ") private String version; / /... The get/set method} copy code is omitted hereCopy the code

A NotNull note will do. Doesn’t it smell good?

Is Map parameter passing really useless?

Some people say that the advantage of using Map to send parameters is that it can be expanded at will, and the later changes are flexible. If you want to fill in a few parameters, you can fill in a few parameters. It also saves the trouble of object definition and naming.

If you must use Map to pass parameters

If you can’t avoid using Map for reference, please have a complete test case, so that the maintenance person will not have to look at the code every day and wonder about life.

Through the test case, the maintenance person can also quickly understand the parameter passing and call between the code, otherwise it really can only rely on the imagination to debug.

SHH…

If the Controller layer code of your project is still using Map to pass parameters, promise me, don’t say a word, quickly change all secretly, quick! Speed! Run ahead!

Top recommendations:

Leetcode, written by BAT boss, brush the problem notes, read the algorithm to kill 90% of the second!

Liver for a month, finally completed 240,000 words Java interview manual

Java to master the interview with the most complete information package (including download methods)

Graphic operating system, network, computer composition PDF download!

300 page illustrated web PDF download!

Author: CodeSheep