0
votes
package com.books.bookstore.controller;

import org.springframework.stereotype.Controller;

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/myAccount")
    public String myAccount() {
        return "myAccount";
    }
}

https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE com.bookstore bookstore 0.0.1-SNAPSHOT bookstore This is the description of my project.

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

http://localhost:8081/myAccount

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

Wed Apr 01 14:36:39 EAT 2020 There was an unexpected error (type=Not Found, status=404). No message available

2
Please complete your question. It's not clear what you are askingSimon Martinelli
what is the error that you receive ?Hemant
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Apr 01 14:36:39 EAT 2020 There was an unexpected error (type=Not Found, status=404). No message availablekadrtw
what is URL for api call you are making ?Hemant
Thanks for responding Mr. Hemant KUMAR localhost:8081/myAccountkadrtw

2 Answers

0
votes

Using @GetMapping is another easy solution

@GetMapping("/")
@ResponseBody
public String index() {
    return "index";
}
0
votes

I believe you want to return view from these controller methods like - index.html/index.jsp

Configure view resolver in your project. Here is good article around it. https://www.baeldung.com/spring-mvc-view-resolver-tutorial

Also, if you just need to return JSON response from your APIs then add annotation @ResponseBody on top of your method like -

    @RequestMapping(value = "/")
    @ResponseBody
    public String index() {
        return "index";
    }

P.S. - It is good practice to use appropriate HTTP methods in your APIs

@RequestMapping(value = "/", method = RequestMethod.GET)

Edit:

  • instead of using @RequestMapping with the method parameter, you can use @GetMapping
  • if this is a REST controller instead of adding @ResponseBody to every method, annotate the class with @RestController instead of @Controller.

P.J.Meisch