Skip to content

Commit

Permalink
processing order for Jackson/Jaxb annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Feb 26, 2021
1 parent 84b7eba commit 6d3f47f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,10 +17,11 @@
package org.glassfish.jersey.jackson.internal;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.Module;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.Annotations;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;

import java.util.Objects;
import java.util.List;
import javax.inject.Singleton;

/**
Expand All @@ -29,7 +30,11 @@
@Singleton
public class DefaultJacksonJaxbJsonProvider extends JacksonJaxbJsonProvider {

//do not register JaxbAnnotationModule because it brakes default annotations processing
private static final String EXCLUDE_MODULE_NAME = "JaxbAnnotationModule";

public DefaultJacksonJaxbJsonProvider() {
super();
findAndRegisterModules();
}

Expand All @@ -39,14 +44,16 @@ public DefaultJacksonJaxbJsonProvider(final Annotations... annotationsToUse) {
}

private void findAndRegisterModules() {
final ObjectMapper defaultMapper = _mapperConfig.getDefaultMapper();
if (Objects.nonNull(defaultMapper)) {
defaultMapper.findAndRegisterModules();
}

final ObjectMapper defaultMapper = _mapperConfig.getDefaultMapper();
final ObjectMapper mapper = _mapperConfig.getConfiguredMapper();
if (Objects.nonNull(mapper)) {
mapper.findAndRegisterModules();

final List<Module> modules = ObjectMapper.findModules();
modules.removeIf(mod -> mod.getModuleName().contains(EXCLUDE_MODULE_NAME));

defaultMapper.registerModules(modules);
if (mapper != null) {
mapper.registerModules(modules);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -23,6 +23,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;

@Path("/entity/")
public final class ServiceTest {
Expand All @@ -45,6 +46,7 @@ private static final class EntityTest {
this.value = value;
}

@XmlElement(name = "jaxb")
@JsonGetter("name")
public final String getName() {
return name;
Expand Down

0 comments on commit 6d3f47f

Please sign in to comment.