SSM-Maven+Jackson

Time:2021-1-12

1 Maven overview

Maven is aProject management toolsIt contains a project object model, a set of standards, and aProject life cycle(project lifecycle), a dependency management system, and a system used to run defined in the phase of the life cycleplug-in unit(plugin) logic of goal. When you use maven, you describe your project with a well-defined project object model, and Maven can then apply crosscutting logic from a set of shared (or custom) plug-ins.

Maven is mainly used to solve the problem of package dependency.

In the sub service development of the project, we used to import the jar package to introduce the code. There may be different services with different versions of the same function, resulting in some functions can not be used.
After the introduction of Maven project management tool, the same functions in the service can be extracted upward to form a common dependency Maven parent, and the dependencies in each service inherit Maven parent, so as to unify the management of dependencies, facilitate the management and maintenance, and avoid the package dependency problem.

Note: type Maven parent as POM package<

packaging>pom</packaging>

SSM-Maven+Jackson

2 Jackson

summary:Jackson is a Java class library used to process data in JSON format

Three functions:

  • Jackson core, the core package, provides related APIs based on “stream mode” parsing, including jsonpaser and jsongenerator. Jackson’s internal implementation generates and parses JSON through the high-performance stream mode API’s jsongenerator and jsonparser.
  • Jackson annotations, annotation package, provides standard annotation function;
  • Jackson databind, a data binding package, provides related APIs based on “object binding” parsing (objectmapper) and “tree model” parsing (jsonnode); APIs based on “object binding” parsing and “tree model” parsing rely on APIs based on “flow mode” parsing.

2.1 ObjectMapper

Jackson’s most common API is objectmapper based on “object binding”. Here is a simple example of using objectmapper.

  • Objectmapper serializes Java objects into JSON through writevalue series methods, and stores JSON in different formats, such as string (writevalueasstring), byte array (writevalueasstring), writer, file, stream and dataoutput.
  • Objectmapper deserializes JSON into Java objects from different data sources such as string, byte array, reader, file, URL and InputStream through readvalue series methods.

Transformation of JSON and object

package com.jt.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectMapperUtil {
   /** 
     *1. How to convert an object to a JSON string???
     *Steps:
     *1. Get all the getxxx () methods of the object
     *2. Remove the prefix get of getxxx method to form the key = XXX of JSON
     *3. Get the value of attribute by calling getxxx method, and form the value of JSON
     *4. Use JSON format to splice the data {key: value, key2: Value2}
     *2. How can JSON be transformed into an object???
     *      {lyj:xxx}
     *Steps:
     *1. According to the class parameter type, using java reflection mechanism, instantiate the object
     *2. Parse JSON format to distinguish key:value
     *3. The setlyj() name of the method
     *4. Call setXXX (value) of the object to transfer the data,
     *5. Finally, all the keys in the JSON string are transformed into the attributes of the object
     **/

    private static final ObjectMapper MAPPER = new ObjectMapper();

    //Converting objects to JSON
    public static String toJSON(Object target){
        try {
           return MAPPER.writeValueAsString(target);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    //Converting JSON to objects
    //Requirement: if the user passes what type, what object is returned
    public static <T> T toObject(String json,Class<T> targetClass){
        try {
            return MAPPER.readValue(json, targetClass);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

Recommended Today

Algorithm Engineering 3. Mathematical basis, probability theory and statistics

Traditional machine learning can be said to use probability theory everywhere. probability theory 1. Total probability formula and Bayesian formula Total probability formula$$P(A)=\sum\limits_{j=1}^{n}P(B_j)P(A|B_j)$$Bayes formula$$P(B_i|A)=\dfrac{P(A,B_i)}{P(A)}=\dfrac{P(B_i)P(A|B_i)}{\sum\limits_{j=1}^{n}P(B_j)P(A|B_j)}$$Bayesian formula is the core tool of Bayesian statistics. Bayesian school thinks that the probability of event occurrence is not as simple as frequency school, but should add human priors, so that […]