Search code examples
javaspringspring-bootrestcxf

Can't find the request for http://localhost:8082/{params}'s Observer


I have a very simple Java REST service code concept here.

The problem is that when I am hitting the URL the below message is showing up in the console: Can't find the request for http://localhost:8082/REST/services/patientservices/getallpatients's Observer

I am using cxf.jaxrs.component-scan=true for spring to read the annotations.

Below are the classes and configuration file:

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

Solution

  • There was a mistake in the pom.xml file.

    Instead of jaxws dependency it should be jaxrs dependency.

    <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
                <version>3.4.3</version>
            </dependency>