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\":\"testelascrip1@gmail.com\",\"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

  • 1
    could you add the controller code? – cralfaro Mar 13 '17 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 – cralfaro Mar 13 '17 at 12:27
  • Possible duplicate of [How do I map Spring MVC controller to a uri with and without trailing slash?](http://stackoverflow.com/questions/12043618/how-do-i-map-spring-mvc-controller-to-a-uri-with-and-without-trailing-slash) – john16384 Mar 13 '17 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? – Ansgar Schulte Mar 13 '17 at 12:41
  • @cralfaro here is the controller 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) { } } – Elavarasu Pandiyan Mar 13 '17 at 12:42
  • am sorry about the formatting – Elavarasu Pandiyan Mar 13 '17 at 12:49
  • @john16384 yes but tried all the solutions there. didn't work. – Elavarasu Pandiyan Mar 13 '17 at 12:50
  • @AnsgarSchulte No. I have not set that to false anywhere. – Elavarasu Pandiyan Mar 13 '17 at 12:50
  • @ElavarasuPandiyan can you add to your question (Edit it) the form where are you calling the POST method. Thanks – cralfaro Mar 13 '17 at 14:14

1 Answers1

0

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

john16384
  • 7,800
  • 2
  • 30
  • 44