Preface

MySQL supports fields of type JSON. In comparison to string-type fields, JSON fields offer the following advantages:

  • Automatic validation of JSON syntax
  • Underlying support for quick access to elements within the JSON. There’s no need to read the entire string and then parse it into a JSON object.

Logically, JSON is no different from a POJO. Spring MVC has already implemented automatic conversion between the two at the Controller tier, evident in request parameters and return values. So, how can the Repository tier achieve automatic conversion?

This article demonstrates achieving seamless, non-intrusive mapping (ORM) between JSON and POJO using MyBatis-Plus in the Repository tier.

Demo

Table

Read more »

Preface

The controller methods (handlers) are responsible for validating request parameters.

In traditional approaches, validation of request parameters is performed at the beginning of each method. If any parameter fails to meet the conditions, an exception is thrown, or an HTTP error is returned early.

The Validation API provides a series of annotations. By placing these annotations on the properties of an entity class, Spring MVC automatically performs request parameter validation based on the semantics of these annotations. If a parameter does not meet the conditions, an exception is thrown. This saves developers from manually validating request parameters.

The class responsible for automatic parameter validation is actually imported by Spring Boot through automatic configuration, so we need to use the corresponding starter dependencies.

Demo

Dependencies

Read more »

Custom 404 Page

First, create a post named 404:

1
hexo new 404

In the corresponding markdown file, we can customize the 404 page. Due to the characteristics of the Hexo framework, we can write JS scripts to make the page interactive.

Here is my 404 page. As you can see, there’s a countdown on the page. When the countdown reaches 0, it will automatically redirect to the homepage. Of course, users can also manually click to return to the homepage.

Below is the code, which can be directly placed in the markdown file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script src="./../js/jquery-3.3.1.min.js"></script>

<div align="center">You are visiting a non-existent address 🤔</div>
<div align="center">You will be automatically redirected to the homepage in <span id="seconds">10</span> seconds,</div>
<div align="center">Or you can click this <a target="_self" href="/en">link</a> to redirect now!</div>

<script>
$(function () {
setInterval(function () {
var seconds = $("#seconds").text();
$("#seconds").text(--seconds);
if (seconds == 0) {
location.href = "/en";
}
}, 1000);
});
</script>

I chose to host the jQuery code locally for faster response.

Here, jQuery is only used to manipulate the DOM, so you can also write it using native JS functions.

How to Redirect to a Custom 404 Page?

Read more »
0%