Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Should Spring Boot Mapping be letter for letter when referencing a file to return?

Hi guys,

Using Spring Boot, I was wondering when using the @GetMapping() method, is it a good practice to do letter-for-letter reference when returning a particular file such an HTML, or have more practical text in the URL?

Example:

Code
@GetMapping("/favourite-apps")
public String goFavApps() {
    return "favapps.html";
}

or should it be strictly what the file name is?
I would say prefer easy to read, but still relatively short URLs. "/favourite-apps" is good. The main reason I say that is that it's a better user experience for whoever is visiting your site, and it can improve search engine rankings. For internal file names (e.g. "favapps.html"), it's purely up to your preference. Whatever style you pick for that, just stay consistent.
If you want to do a redirection, it's not done properly.

You can use ModelAndView Object to redirect :
Code
@GetMapping("/favourite-apps")
public ModelAndView goFavApps() {
    return new ModelAndView("redirect:favapps.html");
}

Or HttpServletResponse :
Code
@GetMapping("/favourite-apps")
public void method(HttpServletResponse httpServletResponse) {
    httpServletResponse.setHeader("Location", "favapps.html");
    httpServletResponse.setStatus(302);
}

302 is the HTTP redirection code.

Hmu if you need help
(Oct 2, 2020 12:10 AM)G.P.D. Wrote:  If you want to do a redirection, it's not done properly.

You can use ModelAndView Object to redirect :
Code
@GetMapping("/favourite-apps")
public ModelAndView goFavApps() {
    return new ModelAndView("redirect:favapps.html");
}

Or HttpServletResponse :
Code
@GetMapping("/favourite-apps")
public void method(HttpServletResponse httpServletResponse) {
    httpServletResponse.setHeader("Location", "favapps.html");
    httpServletResponse.setStatus(302);
}

302 is the HTTP redirection code.

Hmu if you need help

Taking note, thank you! I'm learning all this now. I tip my hat to you!
(Oct 2, 2020 10:56 AM)Chukozy Wrote:  Taking note, thank you! I'm learning all this now. I tip my hat to you!

There is no problem bro.
PM me in case you're stuck while programming !


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by _redbaron_Jun 14, 2024 04:56 PM 16 662 Jun 14, 2024 04:56 PM
Last Post: secret_code
[C] by oh9odDec 7, 2020 04:55 AM 0 171 Dec 7, 2020 04:55 AM
Last Post: oh9od
[Java] by _danielApr 7, 2020 07:25 AM 3 271 Apr 7, 2020 07:25 AM
Last Post: Shprqness