[英]How to fix 'HTTP-415' error, during POST request in REST web service using spring boot
[英]How to fix 'HTTP-404' error, during GET request in REST web service using spring boot
我的示例Spring Boot REST Web服務給出了404錯誤,我不確定出了什么問題
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);
}
}
POSTMan REST請求發件人的我的請求: http:// localhost:8080 / students / stu1 / courses / course1
的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>
響應:
{“ timestamp”:“ 2018-12-28T02:48:00.185 + 0000”,“ status”:404,“ error”:“ Not Found”,“ message”:“無可用消息”,“ path”:“ /學生/ stu1 /課程/課程1“}
假設您在不同的包com.in28minutes.springboot.controller;
具有Controller類com.in28minutes.springboot.controller;
和Spring boot主類在不同的com.in28minutes.springboot.studentservices;
包中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一起添加到Service類中:
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);
}
}
package com.in28minutes.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.in28minutes.springboot")
public class StudentServicesApplication {
public static void main(String[] args) {
SpringApplication.run(StudentServicesApplication.class, args);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.