springboot框架为什么访问localhost:8080状态码为200,访问路径内的depts状态码为404,?

为什么访问 localhost:8080 状态码为 200 ,访问路径内的 depts 状态码为 404

这是controller层

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    //private static Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

 
    @RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式为GET
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        //调用service查询部门数据
        List<Dept> deptList = (List<Dept>) deptService.getAllDepts();
        return Result.success(deptList);
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);
        //调用service删除部门
        deptService.deleteDept(Integer.valueOf(id));
        return Result.success();
    }

这是service层

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public void delete(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void add(Dept dept) {
        deptMapper.insert(dept);
    }


    @Override
    public Result getAllDepts() {
        return null;
    }

    @Override
    public Result deleteDept(Integer id) {
        return null;
    }


    @Override
    public Result createDept(Dept dept) {
        return null;
    }
}
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门: {}" , dept);
        //调用service新增部门
        deptService.createDept(dept);
        return Result.success();
    }
}
public interface DeptService {

    List<Dept> list();

    void delete(Integer id);

    void add(Dept dept);

    Result getAllDepts();

    Result deleteDept(Integer id);


    Result createDept(Dept dept);

}

这是mapper层

@Mapper
public interface DeptMapper {
 
    @Select("select * from dept")
    List<Dept> list();
    @Delete("delete from dept where id = #{id}")
    void deleteById(Integer id);

    @Insert("insert into dept(id,name,permission) values(#{id},#{name},#{permission})")
    void insert(Dept dept);
}

这是domain层

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept {
    private Integer id; //ID
    private String name; //部门名称
    private String permission;//部门权限
}

日志信息如下

2024-05-04 13:34:07.209 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.DispatcherServlet                : GET "/", parameters={}
2024-05-04 13:34:07.209 DEBUG 11888 --- [nio-8080-exec-6] o.s.b.a.w.s.WelcomePageHandlerMapping    : Mapped to ParameterizableViewController [view="forward:index.html"]
2024-05-04 13:34:07.209 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.v.ContentNegotiatingViewResolver : Selected '*/*' given [*/*]
2024-05-04 13:34:07.209 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.v.InternalResourceView           : View name 'forward:', model {}
2024-05-04 13:34:07.209 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.v.InternalResourceView           : Forwarding to [index.html]
2024-05-04 13:34:07.210 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.DispatcherServlet                : "FORWARD" dispatch for GET "/index.html", parameters={}
2024-05-04 13:34:07.210 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping        : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2024-05-04 13:34:07.211 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.DispatcherServlet                : Exiting from "FORWARD" dispatch, status 200
2024-05-04 13:34:07.211 DEBUG 11888 --- [nio-8080-exec-6] o.s.w.s.DispatcherServlet                : Completed 200 OK
2024-05-04 13:34:09.865 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.DispatcherServlet                : GET "/depts", parameters={}
2024-05-04 13:34:09.865 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.h.SimpleUrlHandlerMapping        : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2024-05-04 13:34:09.867 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2024-05-04 13:34:09.867 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.DispatcherServlet                : Completed 404 NOT_FOUND
2024-05-04 13:34:09.868 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.DispatcherServlet                : "ERROR" dispatch for GET "/error", parameters={}
2024-05-04 13:34:09.868 DEBUG 11888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2024-05-04 13:34:09.869 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2024-05-04 13:34:09.869 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Writing [{timestamp=Sat May 04 13:34:09 GMT+08:00 2024, status=404, error=Not Found, message=No message avail (truncated)...]
2024-05-04 13:34:09.870 DEBUG 11888 --- [nio-8080-exec-4] o.s.w.s.DispatcherServlet                : Exiting from "ERROR" dispatch, status 404

希望大神能帮帮我这个初学者

阅读 943
5 个回答
新手上路,请多包涵

如果你使用的是IDEA 建议更新到较新版本
image.png
都会有这样的提示你再看看路径是怎么样的

新手上路,请多包涵

controller层和方法上的requestmapping只需要一个

新手上路,请多包涵

访问 depts/depts 就好了,想访问哪个接口需要 Controller 上 RequestMapping 地址 + 方法上RequestMapping地址

@RequestMapping 用在 @Controller或者 @RestController 注解标识的类上的话,意思是接下来整个类的所有使用 @RequestMapping 的方法的请求前缀都得加上类上面标识的路径。
比如类上面是:

@RequestMapping("/depts")

方法上面也是:

@RequestMapping("/depts")

那么最后应该请求的路径就是 xxx/depts/depts

如果去掉类上面的 @RequestMapping("/depts") 那么最后应该请求的路径就是 xxx/depts

类上的 @RequestMapping 和方法上的 @RequestMapping 两者的路径组合到一起才是完整的接口访问路径,先类再方法,你写的例子的接口地址应该是 http://localhost:8080/depts/depts
@GetMapping@PostMapping@DeleteMapping 等都是同理,因为这些都是继承于 @RequestMapping

另外,如果你在配置文件中还写了 server.servlet.context-path=xxx 的话,那么还需要在访问路径前再加上xxx,即 http://localhost:8080/xxx/depts/depts

最后,你说访问 depts404,应该是指 http://localhost:8080/depts ,而你在controller层并没有对应的接口,因此是404。如果你希望能否访问,那可以去除类上的 @RequestMapping("/depts") 或者 list 接口上的 @RequestMapping(value = "/depts",method = RequestMethod.GET) 其中之一。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏