Skip to content

Commit

Permalink
Remove nil-tag from generated XML
Browse files Browse the repository at this point in the history
Updated XmlConverter to exclude <nil>false</nil> from the XML output. Also, simplified the validate method by removing the need to pass the class type parameter.
  • Loading branch information
krharum committed Sep 4, 2024
1 parent 7ec8fa7 commit e615d78
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import no.nav.testnav.inntektsmeldinggeneratorservice.util.XmlConverter;
import no.nav.testnav.inntektsmeldinggeneratorservice.provider.dto.InntektsmeldingDTO;
import no.nav.testnav.inntektsmeldinggeneratorservice.binding.InntektsmeldingM;
import no.nav.testnav.inntektsmeldinggeneratorservice.provider.dto.InntektsmeldingDTO;
import no.nav.testnav.inntektsmeldinggeneratorservice.util.XmlConverter;
import no.nav.testnav.libs.dto.inntektsmeldinggeneratorservice.v1.rs.RsInntektsmelding;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<String> create(@RequestBody RsInntektsmelding request) {
log.info("Konverterer inntektsmelding til : {}", melding);
String xml = XmlConverter.toXml(melding, InntektsmeldingM.class);

if (!XmlConverter.validate(xml, InntektsmeldingM.class)) {
if (!XmlConverter.validate(xml)) {
log.warn("Validering av opprett xml feilet");
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public static <T> String toXml(JAXBElement<T> value, Class<T> clazz) {
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(value, sw);

String xmlContent = sw.toString().replace("ns2:", "").replace(":ns2", "");
String xmlContent = sw.toString()
.replace("ns2:", "")
.replace(":ns2", "")
.replace("<nil>false</nil>", "");

log.debug("Opprettet xml: {}", xmlContent);
return xmlContent;
Expand All @@ -46,9 +49,9 @@ public static <T> String toXml(JAXBElement<T> value, Class<T> clazz) {
}
}

public static <T> boolean validate(String xml, Class<T> clazz) {
public static boolean validate(String xml) {
try {
toObject(xml, clazz);
toObject(xml);
return true;
} catch (Exception e) {
log.warn("Validering av xml feilet", e);
Expand Down Expand Up @@ -76,8 +79,7 @@ public static LocalDate toLocalDate(LocalDateTime localDateTime) {
return nonNull(localDateTime) ? localDateTime.toLocalDate() : null;
}

@SuppressWarnings("unchecked")
private static <T> void toObject(String xml, Class<T> clazz) {
private static void toObject(String xml) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance("no.nav.testnav.inntektsmeldinggeneratorservice.provider.adapter");

Expand Down

0 comments on commit e615d78

Please sign in to comment.