为什么访问 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
希望大神能帮帮我这个初学者
如果你使用的是IDEA 建议更新到较新版本
都会有这样的提示你再看看路径是怎么样的