Restful example of SSM framework

Demo environment: Maven + Spring + spring MVC + mybatis plus or mybatis + jdk8

I think there should be no problem with JDK7 because it basically uses JDK6 or JDK7 related features.

Of course, jdk10 now has new features and performance upgrades to some original Java classes.

Restful is an architecture style, which manages resource requests by category.

All things are resources, so it can be understood.

There are different request methods corresponding to different requests, such as

If the resource list is obtained, the corresponding resource list information can be obtained through the get request;

If you want to delete a resource, you can use the delete request;

If you want to modify a resource, you can use put request;

If you want to add a resource, you can use post request;

There are only a few commonly used in daily life. For spring MVC, you can enjoy using annotations, such as @ postmapping, @ getmapping, @ putmapping, @ deletemapping, etc.

At the same time, you can also use the method in @ requestmapping to define the required request method. Of course, I personally prefer to use the corresponding request annotation.

If it is developed in the previous way, the corresponding resources are rarely requested.

In addition, there may be questions about why this should be done?

As I said earlier, in order to better manage resources, follow the restful architecture style.

Of course, there are also requests that can distinguish resources, such as acquisition, addition, deletion, modification, etc. Of course, you can do it all with post. As for why post is used, there are a lot of online blogs.

As for delete and put, like post, they can't get specific information through F12, unless they can be obtained through tools such as postman or JMeter. They can also be obtained by using an HTTP class encapsulated in Java.

The purpose of using get is that getting data through get is just a query and does not modify the data. In terms of security, it is not so high. In the case of delete, post and put, the database directly involves deletion, addition and modification, so we must pay special attention to security.

Here are some examples:

1.GET

JS code

2.POST

JS code

3.DELETE

JS code

4.PUT

JS code

Add 403, 415, 500 and Ajax errors

Ajax error:

The reason for this error is a simple syntax error, but the simpler it is, it is often easy to make low-level errors, but it can be debugged through the browser F12. Especially friends from small and medium-sized companies often hold multiple positions, both front-end and back-end, as well as testing and operation and maintenance.

Master reasonable debugging skills and methods. In addition, it should be noted that unit testing and postman or JMeter testing must be timely, so that many problems can be avoided.

2. About this 403

403 the problem is usually a cross domain request problem. The following link can be used as a solution:

https://blog.csdn.net/qq_25152183/article/details/53158222

3.415 error code

The reason for this error code is that, for example, when I want to modify a piece of data, my front-end Ajax does not have a contenttype: 'application / JSON; Charset = UTF-8 'and JSON using this method Stringify (data), but a @ requestbody is added in the background. This annotation also declares that what you pass in the foreground must be JSON, otherwise this problem will occur

four point five zero zero

There are many problems with 500. Only null pointers are listed here. This null pointer problem still comes from modification. Contenttype: 'application / JSON' is added to the foreground; Charset = UTF-8 'and JSON using this method Stringify (data), but @ requestbody is not added in the background, so the parameters cannot be received, resulting in null pointers. Usually, the object is decorated with @ requestbody. If there are only three or fewer parameters, you can directly use {"Param1": Param1, "param2": param2, "param3": param3}. If there are more than three parameters, it is recommended to use the form of data transmission object, that is, dto.

Summary:

From time to time, I still have to take some time to study theory and read some famous foreign computer doctoral articles. Although I don't necessarily understand them completely, I can broaden my horizons, or there will be some unexpected gains from some fragments.

Restful architecture style paper: https://www.ics.uci.edu/ ~fielding/pubs/dissertation/top. htm

You can read it when you have time. After reading it, you may have a very special feeling by looking at the relevant articles shared by other bloggers.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>