簡體   English   中英

使用Springboot REST在Postman上獲取錯誤404

[英]Get error 404 on Postman with Springboot REST

病人服務

@Path("/patientservice")
public interface PatientService {

    @Path("/patients")
    @GET
    List<Patient> getPatients();
}

PatientServiceImpl

@Service
public class PatientServiceImpl implements PatientService {

    Map<Long, Patient> patients = new HashMap<>();
    long currentId = 123;

    public PatientServiceImpl() {
        init();
    }

    void init() {
        Patient patient = new Patient();
        patient.setId(currentId);
        patient.setName("John");
        patients.put(patient.getId(), patient);
    }

    @Override
    public List<Patient> getPatients() {
        Collection<Patient> results = patients.values();
        List<Patient> response = new ArrayList<>(results);
        return response;
    }
}

我嘗試使用localhost:8080 / services / Patientservice / Patients /和localhost:8080 / Patientservice / Patients /作為URL,但仍顯示404錯誤屏幕。 您能幫我調試一下代碼中可能存在的問題嗎? 該應用程序正常運行,沒有錯誤。

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>

    <groupId>com.bharath.restws</groupId>
    <artifactId>restws</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>restws</name>
    <description>Patient REST Services</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.1.11</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-xc</artifactId>
            <version>1.9.13</version>
        </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>

看起來您需要一個可以處理請求的控制器

@RestController
public class PatientController {

    @GetMapping("/patients")
    List<PatientModel> getPatients() {
        return Arrays.asList(
                new PatientModel("1", "john"),
                new PatientModel("2", "donald")
        );
    }

    static class PatientModel {
        private String id;
        private String name;

        // constructor, getters, setters
    }
}

因此,當您請求curl http://localhost:8080/patients ,它將返回一個列表[{"id":"1","name":"john"},{"id":"2","name":"donald"}]

在此控制器中,您可以使用已實施的任何服務

我認為您想要做的是一個REST controller來捕獲您的請求並轉發給您的服務

首先,在您對PatientService接口的實現中添加一個Qualifier ,以便我們可以在控制器中對其進行自動PatientService

@Service
@Qualifier("myImplementation")
public class PatientServiceImpl implements PatientService {
    // your code goes here
}

然后創建您的REST控制器

@RestController
public class PatientController {

    @Autowired
    @Qualifier("myImplementation")
    PatientService patientService;

    @RequestMapping("/patients")
    public List<Patient> getPatients() {
        List<Patient> patients = patientServiceImpl.getPatients();
        return patients;
    }

}

現在,您的/patients端點已映射到控制器的getPatients()方法。 這將依次調用服務上的方法並返回結果。

使用您的實現PatientServiceImpl ,調用http://localhost:8080/patients PatientServiceImpl返回

[{"id":123,"name":"John"}]  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM