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

时间:2018-04-21 09:19:12

标签: java spring rest spring-boot

PatientService

@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 / and 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>

2 个答案:

答案 0 :(得分:0)

看起来你需要一个能处理请求的控制器

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

并且在此控制器中,您可以使用上面已实施的任何服务

答案 1 :(得分:0)

我认为您要做的是REST controller来抓住您的请求并转发到您的服务

首先,在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返回

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