Skip to content

Commit 5525c5d

Browse files
committed
processing order for Jackson/Jaxb annotations
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
1 parent 84b7eba commit 5525c5d

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

media/json-jackson/src/main/java/org/glassfish/jersey/jackson/JacksonFeature.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -46,16 +46,18 @@ public class JacksonFeature implements Feature {
4646
* Using them can provide a useful information to the user, but it can expose unnecessary information, too.
4747
*/
4848
private final boolean registerExceptionMappers;
49+
private final boolean registerJaxbAnnotationModule;
4950

5051
/**
5152
* Default constructor enables registering Jackson's exception mappers
5253
*/
5354
public JacksonFeature() {
54-
this(true);
55+
this(true, false);
5556
}
5657

57-
private JacksonFeature(boolean registerExceptionMappers) {
58+
private JacksonFeature(boolean registerExceptionMappers, boolean registerJaxbAnnotationModule) {
5859
this.registerExceptionMappers = registerExceptionMappers;
60+
this.registerJaxbAnnotationModule = registerJaxbAnnotationModule;
5961
}
6062

6163
/**
@@ -71,7 +73,12 @@ public static JacksonFeature withExceptionMappers() {
7173
* @return JacksonFeature without registered Jackson's exception mappers
7274
*/
7375
public static JacksonFeature withoutExceptionMappers() {
74-
return new JacksonFeature(false);
76+
return new JacksonFeature(false, false);
77+
}
78+
79+
public static JacksonFeature withJaxbAnnotationModule() {
80+
81+
return new JacksonFeature(true, true);
7582
}
7683

7784
private static final String JSON_FEATURE = JacksonFeature.class.getSimpleName();
@@ -93,7 +100,7 @@ public boolean configure(final FeatureContext context) {
93100

94101
// Register Jackson.
95102
if (!config.isRegistered(JacksonJaxbJsonProvider.class)) {
96-
103+
DefaultJacksonJaxbJsonProvider.registerJaxbAnnotationModule.set(registerJaxbAnnotationModule);
97104
if (registerExceptionMappers) {
98105
// add the default Jackson exception mappers
99106
context.register(JsonParseExceptionMapper.class);

media/json-jackson/src/main/java/org/glassfish/jersey/jackson/internal/DefaultJacksonJaxbJsonProvider.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,10 +17,12 @@
1717
package org.glassfish.jersey.jackson.internal;
1818

1919
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.Module;
2021
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.cfg.Annotations;
2122
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
2223

23-
import java.util.Objects;
24+
import java.util.List;
25+
import java.util.concurrent.atomic.AtomicBoolean;
2426
import javax.inject.Singleton;
2527

2628
/**
@@ -29,7 +31,13 @@
2931
@Singleton
3032
public class DefaultJacksonJaxbJsonProvider extends JacksonJaxbJsonProvider {
3133

34+
//do not register JaxbAnnotationModule because it brakes default annotations processing
35+
private static final String EXCLUDE_MODULE_NAME = "JaxbAnnotationModule";
36+
//but if you know what you are doing, you are welcome
37+
public static final AtomicBoolean registerJaxbAnnotationModule = new AtomicBoolean(false);
38+
3239
public DefaultJacksonJaxbJsonProvider() {
40+
super();
3341
findAndRegisterModules();
3442
}
3543

@@ -39,14 +47,20 @@ public DefaultJacksonJaxbJsonProvider(final Annotations... annotationsToUse) {
3947
}
4048

4149
private void findAndRegisterModules() {
50+
4251
final ObjectMapper defaultMapper = _mapperConfig.getDefaultMapper();
43-
if (Objects.nonNull(defaultMapper)) {
44-
defaultMapper.findAndRegisterModules();
52+
final ObjectMapper mapper = _mapperConfig.getConfiguredMapper();
53+
54+
final List<Module> modules = ObjectMapper.findModules();
55+
if (!registerJaxbAnnotationModule.get()) {
56+
modules.removeIf(mod -> mod.getModuleName().contains(EXCLUDE_MODULE_NAME));
57+
} else {
58+
registerJaxbAnnotationModule.set(false);
4559
}
4660

47-
final ObjectMapper mapper = _mapperConfig.getConfiguredMapper();
48-
if (Objects.nonNull(mapper)) {
49-
mapper.findAndRegisterModules();
61+
defaultMapper.registerModules(modules);
62+
if (mapper != null) {
63+
mapper.registerModules(modules);
5064
}
5165
}
52-
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.jackson.internal;
18+
19+
import org.glassfish.jersey.jackson.JacksonFeature;
20+
import org.glassfish.jersey.jackson.internal.model.ServiceTest;
21+
import org.glassfish.jersey.server.ResourceConfig;
22+
import org.glassfish.jersey.test.JerseyTest;
23+
import org.junit.Test;
24+
25+
import javax.ws.rs.core.Application;
26+
27+
import static junit.framework.TestCase.assertEquals;
28+
29+
public final class DefaultJacksonJaxbJsonProviderWithJaxbModuleTest extends JerseyTest {
30+
31+
@Override
32+
protected final Application configure() {
33+
return new ResourceConfig(ServiceTest.class).register(JacksonFeature.withJaxbAnnotationModule());
34+
}
35+
36+
@Test
37+
public final void testJavaOptional() {
38+
final String response = target("entity/simple").request().get(String.class);
39+
assertEquals("{\"jaxb\":\"Hello\",\"value\":\"World\"}", response);
40+
}
41+
}

media/json-jackson/src/test/java/org/glassfish/jersey/jackson/internal/model/ServiceTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -23,6 +23,7 @@
2323
import javax.ws.rs.Path;
2424
import javax.ws.rs.Produces;
2525
import javax.ws.rs.core.MediaType;
26+
import javax.xml.bind.annotation.XmlElement;
2627

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

49+
@XmlElement(name = "jaxb")
4850
@JsonGetter("name")
4951
public final String getName() {
5052
return name;

0 commit comments

Comments
 (0)