Abhishek Kumar
Abhishek Kumar

Reputation: 89

Spring boot Rest Api controller getting 404 error

I'm exposing my services by using Rest in Spring boot. I'm exposing my service with 2 different approach first by using cxf jaxrs dependency and other by spring rest dependency. In first approach i'm getting 404 error when i run my services in postman. But in second approach i'm getting my output. Please help me out.

First approach with cxf jaxrs

@Path("/locations")
public interface LocationRestController {

@GET
public List<Location> getLocations();
}
******************************************
@RestController
public class LocationRestControllerImpl implements LocationRestController {

@Autowired
LocationRepos repos;

@Override

public List<Location> getLocations() {

    return repos.findAll();
}}
{
"timestamp": "2018-09-17T03:36:05.283+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/locationweb/locations"
}

Second approach using spring inbuilt dependency

@RestController
@RequestMapping("/locations")
public class LocationRestControllerImpl  {

@Autowired
LocationRepos repos;

@GetMapping
public List<Location> getLocations() {

    return repos.findAll();
}}

Pom file

<?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.project.abhishek</groupId>
<artifactId>student</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>student</name>
<description>Student DAL</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
"pom.xml"   <version>2.1.0.M2</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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>9.0.12</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>2.0.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>1.0.13</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
        <version>3.2.6</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

Upvotes: 2

Views: 2575

Answers (4)

kj007
kj007

Reputation: 6254

Make sure you have CXF configuration and set as a service bean as if you use @RestController you are actually not implementing CXF so to order to handle requests by CXF, you need to configure CXF server.

    @Configuration
    public class CXFConfig {

      @Autowired
      private Bus bus;


      @Bean
      public Server rsServer() {
         final JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
         endpoint.setBus(bus);
         endpoint.setAddress("/");
         endpoint.setServiceBeans(Arrays.asList(LocationRestController ()));
         return endpoint.create();
      }

    @Bean
    public LocationRestController locationController() {
       return new LocationRestControllerImpl();
       }

    }

Note: No need to implement a controller, you can have directly controller.

Upvotes: 0

GauravRai1512
GauravRai1512

Reputation: 844

please refer below code for correct implementation

public interface LocationRestController {

public List<Location> getLocations();
}
******************************************
@RestController(value="/locationweb/locations")
public class LocationRestControllerImpl implements LocationRestController {

@Autowired
LocationRepos repos;

@Override
@GetMapping
public List<Location> getLocations() {

    return repos.findAll();
}}
{

Upvotes: 1

Mohit Sharma
Mohit Sharma

Reputation: 380

Try this example, https://dzone.com/articles/spring-boot-building-restful-web-services-with-jersey

Most probably you are missing Spring/Jersey integration or Jersey Configuration.

Do add Jersey Configuration for Spring boot to understand Jersey.

Below is the code for Jersey Configuration for convenience.

@Configuration
@ApplicationPath("rest")
public class Config extends ResourceConfig {

public Config() {       
}

@PostConstruct
public void setUp() {
    register(Controller.class);
}
}

Upvotes: 0

MyTwoCents
MyTwoCents

Reputation: 7622

This line might be the issue

@RestController
public class LocationRestControllerImpl implements LocationRestController {

remove @RestController from the class in 1st approach

Upvotes: 0

Related Questions