1

I am working on a checkout page that requires a shipping address and a billing address. This integrates with a third-party library which has these both implemented as the same type: Address. What I need to be able to do is something like:

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Response createOrder(
    @ModelAttribute("customer") Customer customer,
    @ModelAttribute("shipping") Address  shippingAddress,
    @ModelAttribute("payment")  Payment  paymentInformation,
    @ModelAttribute("billing")  Address  billingAddress
) {
    // Create the order
}

I am stuck on how to send two separate models of the same exact type over to my Spring application in a way that makes this work. I could potentially make facade models and map them to the real ones inside of the controller, but I would prefer not to go down that route if I can avoid it.

Edit: Changed the model attribute names to hopefully make the problem area more clear.

2
  • I don't understand the problem. Is there some reason why you must use the same ModelAttribute name? Why cant you just name the model attributes to shippingAddress and billingAddress? The type of the method parameter should not matter Commented May 26, 2016 at 17:41
  • The problem is more in how to send the data over; they have exactly the same field names, so city=Atlanta&state=Georgia etc. is not going to work. Does that make sense? Commented May 26, 2016 at 18:06

1 Answer 1

1

Instead of creating separate model attributes for each type, create an Order object and model attribute 'order.'

 //Order class
 private Customer customer;
 private Address  shippingAddress;
 private Payment  paymentInformation;
 private Address  billingAddress

..

public Response createOrder(
    @ModelAttribute("order") Order order) {
    // Create the order
}

Then the request would look like

shippingAddress.city=foo&billingAddress.city=bar
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.