Page 1 of 1

How to properly extend the API Wrappers.

Posted: Sun Jun 16, 2013 10:15 am
by Sanhi
Hi,

I'm extending the Broadleaf API Wrappers for REST 'CRUD' calls. For this I need to implement several 'unwrap' methods. (First use-case is a Product import feature, that creates a Product entity based on the incoming ProductWrapper on a POST/PUT REST call.)

The problem I have is, how to properly extend the base Wrappers. I have implemented a new Bean that extends the base wrapper, but due to JAXB processing the Child fields always refer to the original implementation.

For example:

Code: Select all


@XmlRootElement(name = "product")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class ProductWrapper extends BaseWrapper implements APIWrapper<Product> {

    @XmlElement
    protected ProductSummaryWrapper productSummary;

    ....

}


In this particular case there is no way to override ProductSummaryWrapper for a custom wrapper that can use this field to 'unwrap'. This wouldn't be such a problem, when the the field access wasn't 'protected'. Now I don't see a solution to implement a proper 'unwrap'.

Ofcourse I can't cast this productSummary to a custom Wrapper class. (ClassCastException), but I do need the internals to properly construct a Product.

The only solution I see now is to change the Broadleaf Core so the field access is from protected -> public to access the internals. (Id, description, etc).

Maybe I'm missing something I can do to properly extend the Wrappers, without modifying the Broadleaf core?

Thanks in advance,

Sander

Re: How to properly extend the API Wrappers.

Posted: Thu Jun 20, 2013 6:59 pm
by ktisdell
You can extend the wrapper class and implement APIUnwrapper. Or, you can implement your own JAXB class to handle deserialization. Either of these methods will work.

Re: How to properly extend the API Wrappers.

Posted: Thu Jun 20, 2013 7:01 pm
by ktisdell
By the way, you should never modify Broadleaf Core. You can extend the wrapper classes with your own classes by simply overriding the wrappers bean definition with your own in your Spring Application Context merge configuration files.

Re: How to properly extend the API Wrappers.

Posted: Fri Jun 21, 2013 2:52 pm
by Sanhi
Hi,

Thanks for your reply. Sure I don't want to modify the Broadleaf Core, but in this scenario there is no way to actually override the ProductSummaryWrapper.

For example:

Code: Select all

public class MyProductWrapper extends ProductWrapper implements APIUnwrapper<Product>{

   public Product unwrap( ..... ) {
   
    // do some base unwrapping...

    // Proceed into ProductSummary
    // Does not work, ProductSummary is of type ProductSummaryWrapper
    MyProductSummaryWrapper mySummaryWrapper = this.productSummary;

   }
 
}


In my opinion there is no way to override ProductSummaryWrapper here, because the JAXB processing always maps to a ProductSummaryWrapper concrete class. At that stage you're not able to unwrap ProductSummaryWrapper or access it's internals.

I have tried overriding the @XmlElement for productSummary in the MyProductWrapper, but that didn't work either.

So what am I missing here?

Thanks,

Sander