79655252

Date: 2025-06-05 22:24:03
Score: 1.5
Natty:
Report link

There is my example of your problem solution - https://github.com/Baranovich/customer. This is full spring boot project for your services interaction case.

I'm parse XML using JAXB with deserialization from json to CustomerType object and also get the same CustomerType from other json (see the request examples):

Example-1

Url:
POST http://localhost:8080/customer/post_customer_type_object

Body:

"Standard"

enter image description here

This JSON data deserialized to object class:

package gu.ib.customer.domain;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;
import javax.xml.bind.annotation.XmlType;

@Slf4j
@XmlType(name = "CustomerType", namespace = "http://www.my.personal.schema")
@XmlEnum
@RequiredArgsConstructor
public enum CustomerType {

    @XmlEnumValue("Standard")
    STANDARD("Standard"),

    @XmlEnumValue("Premium")
    PREMIUM("Premium");
    private final String value;

    public String value() {
        return value;
    }

    @JsonCreator
    public static CustomerType fromValue(String v) {
        for (CustomerType c: CustomerType.values()) {
            if (c.value.equals(v)) {
                return c;
            }
        }
        log.debug("Invalid value for CustomerType: {}", v);
        throw new IllegalArgumentException(v);
    }
}

Example-2

Url:
POST http://localhost:8080/customer/post_customer_type_object

Body:

{
    "data": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Customer xmlns=\"http://www.my.personal.schema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.my.personal.schema\">Premium</Customer>"
}

enter image description here

This JSON data is deserialized to xml content string with further transformation into the same class - CustomerType. I think it is correct example of solution for your case.

If you probably want to send XML type (not as xml content string inside JSON), you can just change receiving data format on controller layer.

If you probably want to send some JSON node with particular node name (not simple string, which is a special case of JSON, like in Example 1), you should change fromValue method to process JsonNode instead of String.

Reasons:
  • Blacklisted phrase (1): enter image description here
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Baranovich