Java 如何修复';HTTP-404';错误,在REST web服务中使用spring引导获取请求期间
我的示例SpringBootRESTWeb服务给出了404错误,我不确定哪里出了问题Java 如何修复';HTTP-404';错误,在REST web服务中使用spring引导获取请求期间,java,rest,spring-boot,Java,Rest,Spring Boot,我的示例SpringBootRESTWeb服务给出了404错误,我不确定哪里出了问题 package com.in28minutes.springboot.studentservices; @SpringBootApplication public class StudentServicesApplication { public static void main(String[] args) { SpringApplication.run(StudentServicesApplica
package com.in28minutes.springboot.studentservices;
@SpringBootApplication
public class StudentServicesApplication {
public static void main(String[] args) {
SpringApplication.run(StudentServicesApplication.class, args);
}
}
package com.in28minutes.springboot.controller;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students/{studentId}/courses")
public List<Course> retrieveCoursesForStudent(@PathVariable String
studentId) {
return studentService.retrieveCourses(studentId);
}
@GetMapping("/students/{studentId}/courses/{courseId}")
public Course retrieveDetailsForCourse(@PathVariable String studentId,
@PathVariable String courseId) {
return studentService.retrieveCourse(studentId, courseId);
}
}
包com.in28minutes.springboot.studentservices;
@SpringBoot应用程序
公共类学生服务应用程序{
公共静态void main(字符串[]args){
运行(StudentServicesApplication.class,args);
}
}
包com.in28minutes.springboot.controller;
@RestController
公共班级学生控制员{
@自动连线
私人学生服务学生服务;
@GetMapping(“/students/{studentId}/courses”)
公共列表retrieveCoursesForStudent(@PathVariable String
学生ID){
返回studentService.retrieveCourses(studentId);
}
@GetMapping(“/students/{studentId}/courses/{courseId}”)
公共课程retrieveDetailsForCourse(@PathVariable String studentId,
@路径变量(字符串ID){
return studentService.retrieveCourse(studentId,courseId);
}
}
我的请求来自邮递员休息请求发件人:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot</groupId>
<artifactId>student-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-services</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.1.1.1发布
com.in28minutes.springboot
学生服务
0.0.1-快照
学生服务
SpringBoot的演示项目
1.8
org.springframework.boot
弹簧靴起动器执行器
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧靴开发工具
运行时
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
答复:
{
“时间戳”:“2018-12-28T02:48:00.185+0000”,
“状态”:404,
“错误”:“未找到”,
“消息”:“没有可用消息”,
“路径”:“/学生/stu1/课程/course1”
}假设在不同的包中有控制器类
com.in28minutes.springboot.Controller
和Spring boot主类在不同的包中com.in28minutes.springboot.studentservices代码>
@SpringBootApplication
默认情况下,@SpringBootApplication
将仅从声明此注释的类的包中进行扫描
这是一个方便的注释,相当于声明@Configuration、@EnableAutoConfiguration和@ComponentScan
如果未定义特定的包,将从声明此注释的类的包中进行扫描
使用@ComponentScan
扫描控制器包
@ComponentScan(basePackages = {"com.in28minutes.springboot.controller"})
@SpringBootApplication
public class StudentServicesApplication {
public static void main(String[] args) {
SpringApplication.run(StudentServicesApplication.class, args);
}
}
更多信息:假设在不同的包com.in28minutes.springboot.Controller中有控制器类
和Spring boot主类在不同的包中com.in28minutes.springboot.studentservices代码>
@SpringBootApplication
默认情况下,@SpringBootApplication
将仅从声明此注释的类的包中进行扫描
这是一个方便的注释,相当于声明@Configuration、@EnableAutoConfiguration和@ComponentScan
如果未定义特定的包,将从声明此注释的类的包中进行扫描
使用@ComponentScan
扫描控制器包
@ComponentScan(basePackages = {"com.in28minutes.springboot.controller"})
@SpringBootApplication
public class StudentServicesApplication {
public static void main(String[] args) {
SpringApplication.run(StudentServicesApplication.class, args);
}
}
更多信息:问题已经解决,@Component需要添加到服务类,以及主应用程序类中的@ComponentScan:
package com.in28minutes.springboot.service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
import com.in28minutes.springboot.model.Course;
import com.in28minutes.springboot.model.Student;
@Component
public class StudentService {
public List<Course> retrieveCourses(String studentId) {
Map<String, Course> courses = Student.getStudentObj(studentId).getCourses();
List<Course> courseList =
courses.values().parallelStream().collect(Collectors.toList());
return courseList;
}
public Course retrieveCourse(String studentId, String courseId) {
return Student.getStudentObj(studentId).getCourses().get(courseId);
}
问题已解决,@Component需要与主应用程序类中的@ComponentScan一起添加到服务类:
package com.in28minutes.springboot.service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;
import com.in28minutes.springboot.model.Course;
import com.in28minutes.springboot.model.Student;
@Component
public class StudentService {
public List<Course> retrieveCourses(String studentId) {
Map<String, Course> courses = Student.getStudentObj(studentId).getCourses();
List<Course> courseList =
courses.values().parallelStream().collect(Collectors.toList());
return courseList;
}
public Course retrieveCourse(String studentId, String courseId) {
return Student.getStudentObj(studentId).getCourses().get(courseId);
}
我试着运行你的代码,但我没有得到404。你确定这个应用程序托管在8080上吗?你能发布堆栈跟踪和pom.xml吗?用pom.xml和http响应更新了帖子。@SoumaliChatterjee你能直接从chrome调用吗?你的服务器。port=8080
在应用程序属性中吗?我试着运行你的代码,但没有得到404。你确定这个应用程序托管在8080上吗?你能发布堆栈跟踪和pom.xml吗?用pom.xml和http响应更新了帖子。@SoumaliChatterjee你能直接从chrome调用吗?你的服务器。port=8080
在应用程序属性中吗?我打算建议OP添加“映射”从他的创业日志到他的帖子。但是你首先回答了关于“包”不匹配的问题。你完全正确!我祈祷它能解决问题:)谢谢你,它应该能解决问题,因为很明显他的包裹没有整理好@PaulsM4感谢你的精彩跟进。已解决问题,如下所示。谢谢!这帮了我很大的忙。我打算建议OP把他启动日志中的“映射”行添加到他的帖子中。但是你首先回答了关于“包”不匹配的问题。你完全正确!我祈祷它能解决问题:)谢谢你,它应该能解决问题,因为很明显他的包裹没有整理好@PaulsM4感谢你的精彩跟进。已解决问题,如下所示。谢谢!这对我帮助很大。