I have POST and GET methods, but when I hide the get method why does an error occurs?(The post method should run until the page get dispatched to GET)

I am new to spring boot. I have get and post methods in my controller class.
But when I hide the get method I think the post method should run without an error until i hit submit. I have thyme leaf and spring-web dependencies.

when i use both post and get method it works fine.

singer.html page:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ENTER SINGER NAME
Enter Singer name
Enter Singer name
Enter Singer name
</code>
<code>ENTER SINGER NAME Enter Singer name Enter Singer name Enter Singer name </code>
ENTER SINGER NAME
Enter Singer name

Enter Singer name

Enter Singer name

Songs.html page:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CHALEYA
HERIYE HERIYE
MUSKURANE KE WAJAH
PHIR BHI TUMKO CHAHUNGA
</code>
<code>CHALEYA HERIYE HERIYE MUSKURANE KE WAJAH PHIR BHI TUMKO CHAHUNGA </code>
CHALEYA

HERIYE HERIYE

MUSKURANE KE WAJAH

PHIR BHI TUMKO CHAHUNGA

But when i comment out the get mapping method in the controller class I get an error:

Error

when I reload my localhost:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Sep 12 21:32:45 IST 2024
There was an unexpected error (type=Method Not Allowed, status=405).

I get the following error in console:

Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘GET’ is not supported]

Controller class: (GetMapping method commented out)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>package com.aman.music.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.aman.music.Singers;
@Controller
public class MyController {
@PostMapping("/")
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
return "Songs" ;
}
// @GetMapping("/")
// public String hii(Model model) {
// model.addAttribute("this",new Singers());
// return "singers";
// }
}
</code>
<code>package com.aman.music.Controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import com.aman.music.Singers; @Controller public class MyController { @PostMapping("/") public String home(@ModelAttribute Singers ss,Model model){ model.addAttribute("this",ss); return "Songs" ; } // @GetMapping("/") // public String hii(Model model) { // model.addAttribute("this",new Singers()); // return "singers"; // } } </code>
package com.aman.music.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.aman.music.Singers;

@Controller
public class MyController { 
    @PostMapping("/")
    public String home(@ModelAttribute Singers ss,Model model){
        model.addAttribute("this",ss);
        return "Songs" ;
    }
//  @GetMapping("/")
//  public String hii(Model model) {
//      model.addAttribute("this",new Singers());
//      return "singers";
//  }
}

These are my files:

MusicPlayerApplication.java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>package com.aman.music;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MusicPlayerApplication {
public static void main(String[] args) {
SpringApplication.run(MusicPlayerApplication.class, args);
}
}
</code>
<code>package com.aman.music; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MusicPlayerApplication { public static void main(String[] args) { SpringApplication.run(MusicPlayerApplication.class, args); } } </code>
package com.aman.music;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MusicPlayerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MusicPlayerApplication.class, args);
    }

}

Controller.java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>package com.aman.music.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.aman.music.Singers;
@Controller
public class MyController {
@PostMapping("/")
public String home(@ModelAttribute Singers ss,Model model){
model.addAttribute("this",ss);
return "Songs" ;
}
@GetMapping("/")
public String hii(Model model) {
model.addAttribute("this",new Singers());
return "singers";
}
}
</code>
<code>package com.aman.music.Controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import com.aman.music.Singers; @Controller public class MyController { @PostMapping("/") public String home(@ModelAttribute Singers ss,Model model){ model.addAttribute("this",ss); return "Songs" ; } @GetMapping("/") public String hii(Model model) { model.addAttribute("this",new Singers()); return "singers"; } } </code>
package com.aman.music.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.aman.music.Singers;

@Controller
public class MyController {

    @PostMapping("/")
    public String home(@ModelAttribute Singers ss,Model model){
        model.addAttribute("this",ss);
        return "Songs" ;
    }
    @GetMapping("/")
    public String hii(Model model) {
        model.addAttribute("this",new Singers());
        return "singers";
    }   
}

Singers.java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>package com.aman.music;
public class Singers {
private String ArjitSingh;
private String Singer;
private String hello;
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
public String getArjitSingh() {
return ArjitSingh;
}
public void setArjitSingh(String arjitSingh) {
ArjitSingh = arjitSingh;
}
public String getSinger() {
return Singer;
}
public void setSinger(String singer) {
Singer = singer;
}
@Override
public String toString() {
return "Singers [ArjitSingh=" + ArjitSingh + ", Singer=" + Singer + ", hello=" + hello + "]";
}
}
</code>
<code>package com.aman.music; public class Singers { private String ArjitSingh; private String Singer; private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } public String getArjitSingh() { return ArjitSingh; } public void setArjitSingh(String arjitSingh) { ArjitSingh = arjitSingh; } public String getSinger() { return Singer; } public void setSinger(String singer) { Singer = singer; } @Override public String toString() { return "Singers [ArjitSingh=" + ArjitSingh + ", Singer=" + Singer + ", hello=" + hello + "]"; } } </code>
package com.aman.music;
public class Singers {
    private String ArjitSingh;
    private String Singer;
    private String hello;

    public String getHello() {
        return hello;
    }
    public void setHello(String hello) {
        this.hello = hello;
    }
    public String getArjitSingh() {
        return ArjitSingh;
    }
    public void setArjitSingh(String arjitSingh) {
        ArjitSingh = arjitSingh;
    }
    public String getSinger() {
        return Singer;
    }
    public void setSinger(String singer) {
        Singer = singer;
    }
    @Override
    public String toString() {
        return "Singers [ArjitSingh=" + ArjitSingh + ", Singer=" + Singer + ", hello=" + hello + "]";
    }   
}

Songs.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Songs List</title>
</head>
<body>
<p>
<p><a href="https://www.youtube.com/watch?v=VAdGW7QDJiU">CHALEYA</a>
<p><a href="https://www.youtube.com/watch?v=RLzC55ai0eo">HERIYE HERIYE</a>
<p><a href="https://www.youtube.com/watch?v=HQ4Ox7mLqds">MUSKURANE KE WAJAH</a>
<p><a href="https://www.youtube.com/watch?v=_iktURk0X-A">PHIR BHI TUMKO CHAHUNGA</a>
</body>
</html>
</code>
<code><!DOCTYPE html> <html xmlns:th="https://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Songs List</title> </head> <body> <p> <p><a href="https://www.youtube.com/watch?v=VAdGW7QDJiU">CHALEYA</a> <p><a href="https://www.youtube.com/watch?v=RLzC55ai0eo">HERIYE HERIYE</a> <p><a href="https://www.youtube.com/watch?v=HQ4Ox7mLqds">MUSKURANE KE WAJAH</a> <p><a href="https://www.youtube.com/watch?v=_iktURk0X-A">PHIR BHI TUMKO CHAHUNGA</a> </body> </html> </code>
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Songs List</title>
</head>
<body>
<p>
<p><a href="https://www.youtube.com/watch?v=VAdGW7QDJiU">CHALEYA</a>
<p><a href="https://www.youtube.com/watch?v=RLzC55ai0eo">HERIYE HERIYE</a>
<p><a href="https://www.youtube.com/watch?v=HQ4Ox7mLqds">MUSKURANE KE WAJAH</a>
<p><a href="https://www.youtube.com/watch?v=_iktURk0X-A">PHIR BHI TUMKO CHAHUNGA</a>
</body>
</html>

singers.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8" http-equiv="Content-Type">
<title>Singer</title>
</head>
<body>
<h1>ENTER SINGER NAME</h1>
<form action="#" th:action="@{/}" th:object="${this}" method="post">
<p>Enter Singer name<input type="text" th:field="*{ArjitSingh}"/>
<p>Enter Singer name<input type="text" th:field="*{Singer}"/>
<p>Enter Singer name<input type="text" th:field="*{hello}"/>
<p><input type="submit" value="SUBMIT"/>
</form>`
</body>
</html>
</code>
<code><!DOCTYPE html> <html xmlns:th="https://www.thymeleaf.org"> <head> <meta charset="UTF-8" http-equiv="Content-Type"> <title>Singer</title> </head> <body> <h1>ENTER SINGER NAME</h1> <form action="#" th:action="@{/}" th:object="${this}" method="post"> <p>Enter Singer name<input type="text" th:field="*{ArjitSingh}"/> <p>Enter Singer name<input type="text" th:field="*{Singer}"/> <p>Enter Singer name<input type="text" th:field="*{hello}"/> <p><input type="submit" value="SUBMIT"/> </form>` </body> </html> </code>
<!DOCTYPE html>
    <html xmlns:th="https://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8" http-equiv="Content-Type">
    <title>Singer</title>
    </head>
    <body>
    <h1>ENTER SINGER NAME</h1>
    <form action="#" th:action="@{/}" th:object="${this}" method="post">
    <p>Enter Singer name<input type="text" th:field="*{ArjitSingh}"/>
    <p>Enter Singer name<input type="text" th:field="*{Singer}"/>
    <p>Enter Singer name<input type="text" th:field="*{hello}"/>
    <p><input type="submit" value="SUBMIT"/>
    </form>`

    </body>
    </html>

Kindly ignore the variables and file names i am using

1

When you open a URL in the browser the HTTP method is always GET. Since you don’t have any GET mapping in your application you get an error.

If you don’t have a handler for error mapping (/error) you get White Label error on the browser.

You need GET mapping to load the singers.html file otherwise you will get error.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật