0

I have a spring MVC application with following details

  1. war file name is forms.war. url pattern in web.xml is"/"
  2. Controller action's @RequestMapping is "/"
  3. RequestMethod.GET actions work properly if localhost:8080/forms is hit
  4. RequestMethod.POST actions not triggered if the post data is hit against localhost:8080/forms
  5. The POST requests gives a 302 redirect
  6. When I hit localhost:8080/forms/ the POST requests work properly

Any solution to make POST request work without trailing slash?

Here is the code I used to test the POST api:

public class HttpPostExample {
    public static void main(String[] args) {
        HttpClient httpClient = HttpClientBuilder.create().build();
        try {

            HttpPost request = new HttpPost("http://localhost:8080/forms");
            StringEntity params =new StringEntity("{\"form_url\":\"admin\",\"website\":\"US_WEBSITE\",\"email\":\"[email protected]\",\"cis_name\":\"testscrip1\"} ");
            request.addHeader("content-type", "application/x-www-form-urlencoded");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            System.out.println("Printing the response code " + response.getStatusLine().getStatusCode());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Changing the url to /forms/ works for POST request but not /forms

10
  • 1
    could you add the controller code? Commented Mar 13, 2017 at 12:25
  • probably the difference its because in the GET request you are not sending parameters, so doesnt mind you add or not the slash, but in the POST you will add some parameters, and you need to add a proper URL action to attach all parameters Commented Mar 13, 2017 at 12:27
  • Possible duplicate of How do I map Spring MVC controller to a uri with and without trailing slash? Commented Mar 13, 2017 at 12:32
  • You can control this behavior globally by using org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.setUseTrailingSlashMatch(boolean). Have you set this to false somewhere? Commented Mar 13, 2017 at 12:41
  • @cralfaro here is the controller code <code> \@Controller public class JSONController { \@RequestMapping(value="/",method = RequestMethod.GET) public \@ResponseBody String handleGET(HttpServletRequest request) { } \@RequestMapping(value="/",method = RequestMethod.POST) public \@ResponseBody String handlePOST(HttpServletRequest request) { } } </code> Commented Mar 13, 2017 at 12:42

1 Answer 1

0

Change @RequestMapping to either no mapping (so don't put a value at all not even "/") or to "/*".

Sign up to request clarification or add additional context in comments.

2 Comments

Tried both. Didn't work. Like not even having the value in RequestMapping and also the value "/*".
The GET requests work fine as per the link you have shared. What am facing issue is with the POST request. It just gets a 302. But the POST request with /forms/ works fine.

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.