2017-10-13 63 views
0

我想弄清楚這個很奇怪的問題。爲什麼從Firefox發佈到Spring Boot應用程序獲得405方法不允許

我有一個使用Thymeleaf作爲模板的Spring Boot應用程序,並且在我的頁面上,我有HTTP POST,在Safari和Chrome瀏覽器上可以很好地工作,但不在Firefox上。

當我嘗試從Firefox張貼到我的春節,引導與這樣的控制器:

@Controller 
public class LoginController { 

    @PostMapping("/login") 
    public String post(@RequestBody LoginForm loginForm) { 
      //process logging in, never makes it here when POSTing from Firefox 
    } 

    @GetMapping("/login") 
    public String get(Model model) { 
    //this generates the Thymeleaf template named login.html 
    return "login"; 
    } 
} 

我曾在一些例子在網上看了如開關PostMapping到RequestMapping,但它仍然無法正常工作。

這是一種嘗試POST前端Thymeleaf的模板:

<form action="#" th:action="@{/login}" method="post" th:object="${loginform}"> 
      <div class="form-group"> 
       <label for="email">Email address:</label> 
       <input type="email" class="form-control" id="email" th:field="*{username}" autofocus="true"/> 
      </div> 
      <div class="form-group"> 
       <label for="password">Password:</label> 
       <input type="password" class="form-control" id="password" th:field="*{password}"/> 
      </div> 
      <button type="submit" class="btn btn-primary">Log in</button><br/> 
    </form> 

所以,實際生成的HTML頁面上,它看起來像:

<form action="/login" method="post"> 
      <div class="form-group"> 
       <label for="email">Email address:</label> 
       <input type="email" class="form-control" id="email" autofocus="true" name="username" value=""> 
      </div> 
      <div class="form-group"> 
       <label for="password">Password:</label> 
       <input type="password" class="form-control" id="password" name="password" value=""> 
      </div> 
      <button type="submit" class="btn btn-primary">Log in</button><br> 
     <input type="hidden" name="_csrf" value="12345"> 
</form> 

我還能嘗試一下呢?謝謝!

+0

你可以分享如何調用這個控制器。分享該代碼也可以瞭解問題。 – Balasubramanian

+0

是的,我將用Thymeleaf HTML模板更新原始文章 – user1805458

+0

您可以在運行時向我們顯示http數據包或HTML文檔中的表單元素嗎? – herokingsley

回答

0

您需要使用@RestController而不是@Controller註釋您的控制器。

@RestController 
public class LoginController { 

    @PostMapping("/login") 
    public String post(@RequestBody LoginForm loginForm) { 
      //process logging in, never makes it here when POSTing from Firefox 
    } 
} 

通常爲REST,你會通過路徑變量請求的實體,如:

@GetMapping("https://stackoverflow.com/users/{id}") 
public ResponseEntity getUser(@PathVariable("id") Long id) { 
    // do something with user identified by its id 
} 

分析與Firefox您的問題,我建議打開與F-12和開發者視圖將這些請求與Chrome中的工作請求進行比較。

+0

嗨堆棧,如果我從@控制器更改爲@ RestController,我的GET端點將fail.I更新與GET部分(和我使用Thymeleaf)的原始帖子。另外,奇怪的是爲什麼POST-ing在Chrome和Safari上運行正常,但不在Firefox上運行? – user1805458

-1

爲了更好的你可以改變@Controller到@RestController,也是在控制器級別添加@RequestMapping,下面添加例如,

@RestController 
@Log4j2 
@RequestMapping("/login") 
public class LoginController { 

@PostMapping("/login")  
public String post(@RequestBody LoginForm loginForm) { 
      //process logging in, never makes it here when POSTing from Firefox 
    } 
} 

讓我知道如果您有任何疑問。

+0

嗨Balasubramanian,如果我將@ Controller更改爲@ RestController,我的GET端點將失敗。我正在使用GET部分更新原始帖子(並且使用Thymeleaf)。另外,奇怪的是爲什麼POST-ing在Chrome和Safari上運行正常,但不在Firefox上運行? – user1805458

+0

你的代碼錯了。或者你字面意思是/登錄/登錄映射?哪個很醜? – Amiko