0
votes

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

1
could you add the controller code? - cralfaro
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
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
@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> - Elavarasu Pandiyan

1 Answers

0
votes

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