ecplise使用maven

下载maven

image-20241027114032085

点击Download

image-20241027114054179

image-20241027114125252

image-20241027114136085

移动压缩包到

image-20241027114252673

image-20241027114340976

image-20241027114410994

image-20241027114419812

image-20241027114436661

image-20241027114454485

image-20241027114517633

找到proxy这一段,在proxies里面添加

   <proxy> 
       <id>workProxy</id> 
       <active>true</active> 
       <protocol>http</protocol>
       <username></username>             
       <password></password> 
       <host>127.0.0.1</host>
       <port>1080</port>   
       <nonProxyHosts>localhost</nonProxyHosts>    
     </proxy>

image-20241027114558237

image-20241027114745102

配置ecplise

image-20241027114212482

image-20241027114823885

image-20241027114851164

image-20241027114924204

image-20241027114935514

image-20241027114949672

image-20241027114955097

image-20241027115022698

image-20241027115029965

image-20241027115040495

创建maven项目

image-20241027115123737

image-20241027115146380image-20241027115402480

点击next

image-20241027115420119

关于这些属性命名规范,见

https://maven.apache.org/guides/mini/guide-naming-conventions.html

image-20241027115631379

image-20241027115702459

点击finish

image-20241027115737356

maven就开始下载构建项目需要的东西了

image-20241027115806071

输入y

image-20241027115820651

image-20241027115856137

image-20241027115925296

使用maven安装包

image-20241027115945347

image-20241027120000077

可以看到dependencies

我们去maven仓库找我们想要的json处理包jackson

image-20241027120056552

image-20241027120107129

点击2.18.0

image-20241027120123589

image-20241027120146886

image-20241027120218225

类似的,我再把其它两个包导入

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.18.0</version>
</dependency>

image-20241027120403380

点击 Update Project

就可以更新了

image-20241027120537835

这里就能看到我们下的包了

然后我们写几个java文件调用一下试试

新建User.java

package com.example.example;

public class User {
    private int id;
    private String name;
    private String email;

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

在Main.java中解析

package com.example.example;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	 String jsonString = "{\"id\": 1, \"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}";

         ObjectMapper objectMapper = new ObjectMapper();

         try {
             // 将 JSON 字符串转换为 User 对象
             User user = objectMapper.readValue(jsonString, User.class);
             
             // 输出用户信息
             System.out.println("ID: " + user.getId());
             System.out.println("Name: " + user.getName());
             System.out.println("Email: " + user.getEmail());
         } catch (Exception e) {
             e.printStackTrace();
         }
    }
}

image-20241027120837346

image-20241027120856724

可以看到调用了库并成功解析json内容到我们的user对象中了

使用maven我们就不需要很麻烦的下载包再手动导入了!