简体   繁体   English

使用Springboot REST在Postman上获取错误404

[英]Get error 404 on Postman with Springboot REST

PatientService 病人服务

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

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

PatientServiceImpl 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;
    }
}

I tried using hitting localhost:8080/services/patientservice/patients/ and localhost:8080/patientservice/patients/ as URLs but still getting 404 error screen. 我尝试使用localhost:8080 / services / Patientservice / Patients /和localhost:8080 / Patientservice / Patients /作为URL,但仍显示404错误屏幕。 Can you please help me debug what might be wrong with the code? 您能帮我调试一下代码中可能存在的问题吗? The application runs properly with no errors. 该应用程序正常运行,没有错误。

pom.xml 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>

Looks like you need a Controller that will handle requests 看起来您需要一个可以处理请求的控制器

@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
    }
}

So when you request patients with curl http://localhost:8080/patients it will return you a list [{"id":"1","name":"john"},{"id":"2","name":"donald"}] 因此,当您请求curl http://localhost:8080/patients ,它将返回一个列表[{"id":"1","name":"john"},{"id":"2","name":"donald"}]

and in this controller you may use any services from above that you have implemented 在此控制器中,您可以使用已实施的任何服务

I think what you want to do is a REST controller to catch your request and to forward to your service 我认为您想要做的是一个REST controller来捕获您的请求并转发给您的服务

First, add a Qualifier to your implementation of the PatientService interface so we can autowire it in the controller 首先,在您对PatientService接口的实现中添加一个Qualifier ,以便我们可以在控制器中对其进行自动PatientService

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

Then create your REST controller 然后创建您的REST控制器

@RestController
public class PatientController {

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

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

}

Now your /patients endpoint is mapped onto the getPatients() method of the controller. 现在,您的/patients端点已映射到控制器的getPatients()方法。 This will in turn call the method on the service and return the result. 这将依次调用服务上的方法并返回结果。

Using your implementation PatientServiceImpl , calling http://localhost:8080/patients returns 使用您的实现PatientServiceImpl ,调用http://localhost:8080/patients PatientServiceImpl返回

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM