-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to provide custom configuration for JAXB context
==== Advanced JAXB-specific features When using the `quarkus-resteasy-reactive-jaxb` extension there are some advanced features that RESTEasy Reactive supports. ===== Inject JAXB components The JAXB resteasy reactive extension will serialize and unserialize requests and responses transparently for users. However, if you need finer usage of the JAXB components, you can inject either the JAXBContext, Marshaller, or Unmarshaller components into your beans: [source,java] ---- @ApplicationScoped public class MyService { @Inject JAXBContext jaxbContext; @Inject Marshaller marshaller; @Inject Unmarshaller unmarshaller; // ... } ---- [NOTE] ==== Quarkus will automatically register all the classes annotated with `@XmlRootElement` to the JAXB context. ==== ===== Customize the JAXB configuration To customize the JAXB configuration for either the JAXB context, and/or the Marshaller/Unmarshaller components, the suggested approach is to define a CDI bean of type `io.quarkus.jaxb.runtime.JaxbContextCustomizer`. An example where a custom module needs to be registered would look like so: [source,java] ---- @singleton public class RegisterCustomModuleCustomizer implements JaxbContextCustomizer { // For JAXB context configuration @OverRide public void customizeContextProperties(Map<String, Object> properties) { } // For Marshaller configuration @OverRide public void customizeMarshaller(Marshaller marshaller) throws PropertyException { marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE); } // For Unmarshaller configuration @OverRide public void customizeUnmarshaller(Unmarshaller unmarshaller) throws PropertyException { // ... } } ---- [NOTE] ==== It's not necessary to implement the three methods, but only the want you need. ==== Alternatively, you can provide your own `JAXBContext` bean by doing: [source,java] ---- public class CustomJaxbContext { // Replaces the CDI producer for JAXBContext built into Quarkus @singleton JAXBContext jaxbContext() { // ... } } ---- [IMPORTANT] ==== By replacing the JAXB context, you need to bound the classes to be serialized/unserialized and also configure the JAXB context accordingly. ====
- Loading branch information
Showing
18 changed files
with
703 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...xb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbClassesToBeBoundBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.jaxb.deployment; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
/** | ||
* List of classes to be bound in the JAXB context. | ||
*/ | ||
public final class JaxbClassesToBeBoundBuildItem extends MultiBuildItem { | ||
|
||
private final List<String> classes; | ||
|
||
public JaxbClassesToBeBoundBuildItem(List<String> classes) { | ||
this.classes = Objects.requireNonNull(classes); | ||
} | ||
|
||
public List<String> getClasses() { | ||
return classes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/CustomJaxbContextCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkus.jaxb.deployment; | ||
|
||
import javax.inject.Singleton; | ||
import javax.xml.bind.Marshaller; | ||
import javax.xml.bind.PropertyException; | ||
|
||
import io.quarkus.jaxb.runtime.JaxbContextCustomizer; | ||
|
||
@Singleton | ||
public class CustomJaxbContextCustomizer implements JaxbContextCustomizer { | ||
@Override | ||
public void customizeMarshaller(Marshaller marshaller) throws PropertyException { | ||
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...sions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/InjectJaxbContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.quarkus.jaxb.deployment; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import java.io.StringWriter; | ||
|
||
import javax.inject.Inject; | ||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Marshaller; | ||
import javax.xml.bind.Unmarshaller; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class InjectJaxbContextTest { | ||
|
||
@Inject | ||
JAXBContext jaxbContext; | ||
|
||
@Inject | ||
Marshaller marshaller; | ||
|
||
@Inject | ||
Unmarshaller unmarshaller; | ||
|
||
@Test | ||
public void shouldInjectJaxbBeans() { | ||
assertNotNull(jaxbContext); | ||
assertNotNull(marshaller); | ||
assertNotNull(unmarshaller); | ||
} | ||
|
||
@Test | ||
public void shouldPersonBeInTheJaxbContext() throws JAXBException { | ||
Person person = new Person(); | ||
person.setFirst("first"); | ||
person.setLast("last"); | ||
|
||
StringWriter sw = new StringWriter(); | ||
marshaller.marshal(person, sw); | ||
|
||
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" | ||
+ "<person>\n" | ||
+ " <first>first</first>\n" | ||
+ " <last>last</last>\n" | ||
+ "</person>\n", sw.toString()); | ||
} | ||
|
||
} |
Oops, something went wrong.