Installer Maven dans visual studio code.

 

Télécharger et Installer Maven

https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.zip

Instruction d'installation

https://maven.apache.org/install.html

 

Créer un projet Maven dans VS Code

 

 

 

 

 

Quand on change le fichier pom.xml

 

package com.example;
import java.io.IOException;

public class Main {
    
    public static void main(String[] args) {
         try {
            // Test
            Person person = new Person("Alice", 30);
            JsonUtil.writeToJsonFile("person.json", person);

            Person readPerson = JsonUtil.readFromJsonFile("person.json", Person.class);
            System.out.println(readPerson.getName() + " - " + readPerson.getAge());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package com.example;
public class Person {
        private String name;
        private int age;

        // Constructeur, getters, setters, etc.
        public Person() {}

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
package com.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;

public class JsonUtil {
      private static final ObjectMapper objectMapper = new ObjectMapper();

    // Écrire l'objet en tant que JSON dans un fichier
    public static void writeToJsonFile(String filename, Object object) throws IOException {
        objectMapper.writeValue(new File(filename), object);
    }

    // Lire l'objet à partir d'un fichier JSON
    public static <T> T readFromJsonFile(String filename, Class<T> valueType) throws IOException {
        return objectMapper.readValue(new File(filename), valueType);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

     <dependencies>
  
        <!-- Ajoutez la dépendance Jackson pour la bibliothèque Jackson JSON -->
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.3</version>
        </dependency>
    </dependencies>

</project>

 

 

 

Modifié le: mardi 24 octobre 2023, 09:35