I’m working on a SpringMVC project.
my jsonController.java:
@RequestMapping("json1")
@RestController //= @Controller+@ResponseBody
public class jsonController {
@RequestMapping("user")
public User data(){
User user = new User();
user.setAge(22);
user.setName("kt");
return user;
}
}
my mvcConfig.java:
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.atguigu.json")
public class mvcConfig implements WebMvcConfigurer {
}
my Init.java:
public class Init extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{mvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
my project structure:
src/
├── main/
│ ├── java/
│ │ └── com.atguigu/
│ │ ├── json/
│ │ │ ├── jsonController.java
│ │ │ └── User.java
│ │ └── config/
│ │ ├── Init.java
│ │ └── mvcConfig.java
│ ├──resources/
│ └── webapp/
│ └──WEB-INF
│ └── web.xml
My pom.xml dependency for JSON:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.0</version>
</dependency>
Environment details:
Apache Tomcat/10.1.33
Windows 11
jdk 21.0.4+8-LTS-274
Spring 6.0.2
servlet.api 9.1.0
When I accessed http://localhost:8080/json1/user, I got this error:
[http-nio-8080-exec-7] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /json1/user
THANK YOU!!!!!!
2
Answers
I solved this issue. The root cause was that I incorrectly configured the Application Context under Run Configuration -> Deployment. This misconfiguration caused the accessible URL for my application to be incorrect. After correcting the Application Context, the problem was resolved.
One issue is that your request mapping is missing the
/
:@RequestMapping("json1")
->@RequestMapping("/json1")
@RequestMapping("user")
->@RequestMapping("/user")
It could help 🙂