Spring Boot支持哪些JSON映射库?

huangapple go评论61阅读模式
英文:

What are the JSON mapper libraries supported by Spring Boot?

问题

The Jackson library is the default integrated JSON mapper library with Spring Boot.

英文:

The Jackson library is the default integrated JSON mapper library with Spring Boot. I want to know what other libraries are available or integrated with Spring Boot, and how to implement them in our Spring Boot application.

答案1

得分: 1

Spring Boot已集成了3个JSON映射器API

  • Jackson(默认)
  • Gson
  • JSON-B

如果我们想要使用Gson,只需在您的pom文件中添加以下依赖:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

但是默认情况下,我们已经实现了Jackson,我们可以使用Maven或编程方式进行排除

使用Maven

在pom文件中,在排除标签下添加starter-json的排除项

<exclusions>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-json</artifactId>
    </exclusion>
</exclusions>

编程方式

@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
public class GsonSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(GsonSpringBootApplication.class, args);
    }
}
英文:

Spring boot has integrated with 3 json mapper api

  • Jackson(default)
  • Gson
  • JSON-B

If we want to use for example Gson then simply add below dependency in your pom file

&lt;dependency&gt;
&lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
&lt;artifactId&gt;gson&lt;/artifactId&gt;
&lt;/dependency&gt;

But we already have by default Jackson implemented which we can exclude either using maven or pragmatically

Using maven

In pom file add exclusion for starter-json under exclusions tag

&lt;exclusions&gt;
            &lt;exclusion&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-starter-json&lt;/artifactId&gt;
            &lt;/exclusion&gt;
        &lt;/exclusions&gt;

programmatically

@SpringBootApplication(exclude = {JacksonAutoConfiguration.class})
public class GsonSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(GsonSpringBootApplication.class, args);
    }
}

huangapple
  • 本文由 发表于 2020年10月22日 04:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64470910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定