BroadleafCommerce / BroadleafCommerce/spring-frameworkmapping

Cannot have an @RequestMapping and @FrameworkMapping mapped to the same endpoint with different HTTP methods

Open
#4 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
2
Forks
2
PR merge metrics
No merged PRs in 30d

Description

The body of this issue was copied over from BroadleafCommerce/Issues#3.

Initially reported at https://stackoverflow.com/questions/45652009/broadleaf-apis-not-working

The example where this fails is CartEndpoint. We have an extension of the Broadleaf CartEndpoint called CustomCartEndpoint in DemoSite. The problem is that we overwrote the exact same URL that exists in the API module. So we essentially have this situation:

@FrameworkMapping(value = "/cart", method = RequestMethod.POST)
 public OrderWrapper createNewCartForCustomer(HttpServletRequest request) {
    ...
}

@RequestMapping(value = "/cart", method = RequestMethod.GET)
public OrderWrapper findCartForCustomer(HttpServletRequest request) {
    ...
}

The problem is when the DispatcherServlet goes looking for a HandlerMapping to resolve the current request, it first consults the default RequestMappingInfoHandlerMapping (which is where all of the @RequestMapping endpoints go). The RequestMappingInfoHandlerMapping essentially only looks for something that matches the URL string and results in a partial mapping for the findCartForCustomer() method (which is a GET). Thus, it throws a 405 method not allowed rather than continue to send the request downstream to be handled by FrameworkMappingHandlerMapping, which is where all of the @FrameworkMapping methods are.

The workaround is to in CustomCartEndpoint in DemoSite extend the createNewCartForCustomer() method, put @RequestMapping on it and just call super. Thus, the final CustomCartEndpoint will look like:

@RestController
@RequestMapping(value = "/cart",
                produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
public class CustomCartEndpoint extends CartEndpoint {

    @Override
    @RequestMapping(value = "", method = RequestMethod.GET)
    public OrderWrapper findCartForCustomer(HttpServletRequest request) {
        try {
            return super.findCartForCustomer(request);
        } catch (Exception e) {
            // if we failed to find the cart, create a new one
            return createNewCartForCustomer(request);
        }
    }
    
    @Override
    @RequestMapping(value = "", method = RequestMethod.POST)
    public OrderWrapper createNewCartForCustomer(HttpServletRequest request) {
        return super.createNewCartForCustomer(request);
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing DispatcherServlet through RequestMappingInfoHandlerMapping and FrameworkMappingHandlerMapping, using the CartEndpoint and CustomCartEndpoint example as the reproduction. Identify where the GET mapping prevents downstream resolution, then verify that equivalent GET and POST mappings can be resolved across both annotations without the workaround described.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.