簡體   English   中英

Java Spring MVC應用程序僅接受帶有斜杠的POST請求

[英]Java Spring MVC application accepts POST requests only with trailing slash

我有一個帶有以下詳細信息的spring MVC應用程序

  1. war文件的名稱為forms.war。 web.xml中的url模式為"/"
  2. 控制器動作的@RequestMapping為"/"
  3. 如果命中localhost:8080 / forms,則RequestMethod.GET操作將正常工作
  4. 如果將發布數據與localhost:8080/forms匹配,則不會觸發RequestMethod.POST操作
  5. POST請求提供302重定向
  6. 當我點擊localhost:8080/forms/ POST請求正常工作

有什么解決方案可以使POST請求工作而不會出現斜杠?

這是我用來測試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();
        }
    }
}

將url更改為/ forms /適用於POST請求,但不適用於/ forms

@RequestMapping更改為無映射(因此,甚至都不要輸入任何值,甚至"/" )或"/*"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM