【发布时间】:2021-08-21 08:34:37
【问题描述】:
我这里有一个非常简单的 Java REST 服务代码概念。
问题是,当我点击 URL 时,控制台中会显示以下消息: 找不到http://localhost:8082/REST/services/patientservices/getallpatients's Observer的请求
我正在使用 cxf.jaxrs.component-scan=true for spring 来读取注释。
下面是类和配置文件:
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
https://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.3.11.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.mayukh.rs</groupId>
<artifactId>jaxrs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jaxrs</name>
<description>JAX RS</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
PatientService.java:
package com.mayukh.rs;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import com.mayukh.rs.model.Patient;
@Path("/patientservices")
public interface PatientService {
@Path("/getallpatients")
@GET
List<Patient> getPatients();
}
PatientServiceImpl.java:
package com.mayukh.rs;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.mayukh.rs.model.Patient;
@Service
public class PatientServiceImpl implements PatientService {
Map<Long, Patient> patients = new HashMap<>();
long currId = 123;
public PatientServiceImpl() {
init();
}
public void init() {
Patient patient = new Patient();
patient.setId(currId);
patient.setName("Mayukh");
patients.put(patient.getId(), patient);
}
@Override
public List<Patient> getPatients() {
Collection<Patient> results = patients.values();
List<Patient> response = new ArrayList<>(results);
return response;
}
}
application.properties:
server.port=8082
cxf.jaxrs.component-scan=true
server.servlet.context-path=/REST
【问题讨论】:
-
您在创建该服务时是否遵循了教程/工具?
-
是的,我正在学习 Udemy 课程
-
您的请求是
/REST/services/patientservices/getallpatients,但您的上下文只是/REST,那么/services子路径来自哪里/它映射到哪里? -
默认情况下 cxf 会暴露 /services 相对 URL 中的所有端点
-
我正在使用 cxf.jaxrs.component-scan=true for spring 来读取注释。你是什么意思?我真的不明白你为什么需要使用 cxf
标签: java spring spring-boot rest cxf