0

I have a webservice that uses immutable classes in action parameters (that's because of other devs working on the project) -- which means no public setters. No public setters means that webservice won't see properties.

The idea was to create private setters and add an init method annotated with PostConstruct to my webservice class. Inside the init method all private setters would be set accessible through reflections.

The problem is that the init method annotated with PostConstruct isn't called at all while deploying. Im using JAX-WS and deploying the project to Glassfish.

ewernli
  • 38,045
  • 5
  • 92
  • 123
Fisher
  • 1,712
  • 1
  • 19
  • 38

1 Answers1

1

What you want to do sounds like a horrible hack.

You problem, if I got it right, is that the objects used as parameters in your actions are immutable. Fortunately, there are tons of way to customize the JAXB mappings with annotations. It should be possible to keep your classes immutable, yet make the fields visible to JAXB.

From this answer, I see:

package blog.immutable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.NONE)
public final class Customer {

    @XmlAttribute
    private final String name;

    @XmlElement
    private final Address address;

    @SuppressWarnings("unused")
    private Customer() {
        this(null, null);
    }

    public Customer(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public Address getAddress() {
        return address;
    }

}

If you don't like the fact that the code above needs a no-arg constructor Customer(), you can have a look at this more complicated approach.

Community
  • 1
  • 1
ewernli
  • 38,045
  • 5
  • 92
  • 123