0

I wrote a spring boot project. It has three files.
Appconfig.java

package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan
(basePackages = {"controller"})
public class AppConfig {
}

ServletInitilizer.java

package config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[0];
    }
    
    @Override
    protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[]{AppConfig.class};
    }
    @Override
    protected String[] getServletMappings() {
    return new String[]{"/"};
    }
}

HelloController.java

package controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class HelloController {
    @RequestMapping("hi")
    @ResponseBody
    public String hi() {
    return "Hello, world.";
    }
}

When I try to run it, it has error "No mapping found for HTTP request with URI [/SpringC1_01/] in DispatcherServlet with name 'dispatcher'". Is this because server didn't find the controller or other reason? Thx.

kk luo
  • 549
  • 1
  • 9
  • 22
  • Try add a @controller annotation for your HelloController – UdonN00dle Jul 02 '21 at 09:17
  • @UdonN00dle. Thx.I tried and it has new error. "Below is a summary of the errors, details of these errors are listed later in the log. * Activation of http://localhost:8080/SpringC1_01/hi resulted in exception. Following failure messages were detected: + Exception reading manifest from http://localhost:8080/SpringC1_01/hi: the manifest may not be valid or the file could not be opened. + Data at the root level is invalid. Line 1, position 1." – kk luo Jul 02 '21 at 09:27
  • Ok, new error is good haha. I haven't seen this error, perhaps try this post? https://stackoverflow.com/questions/15782798/manifest-may-not-be-valid-or-the-file-could-not-be-opened – UdonN00dle Jul 02 '21 at 09:36

2 Answers2

0

Yes. i suspect two issues in the code.

  1. @SpringBootApplication annotation is missing in AppConfig.
  2. @RestController annotation is missing in HelloController.
sathiya raj
  • 35
  • 1
  • 5
0

Most of all you are missing a couple of things here.

  1. Main class which contains public static void main and this class should be annotated with @SpringBootApplication

  2. HelloController should be annotated with @RestController

  3. At method level it should definitely point to some HTTP method in your case perhaprs it is Get mapping, so add @GetMapping annotation arround the method.

  4. Move RequestMapping annotation from method level and add it to HelloController class.