2

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>

2
  • 1
    "But in second approach i'm getting my output." What is the problem then? Commented Sep 17, 2018 at 4:44
  • Traxx the problem is that why i'm not able to develop my rest api with cxf dependency Commented Sep 17, 2018 at 15:47

4 Answers 4

1

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();
}}
{
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Gaurav it work but its similar to my second approach but my question is why i can't do it with cxf dependency by using "@path" and "@get" annotation as "@getmapping" is spring inbuilt binding
Hi Abhishek, As per my understanding @Path is only applicable at implementation level means class level, not interface that's why it's not working.
0

This line might be the issue

@RestController
public class LocationRestControllerImpl implements LocationRestController {

remove @RestController from the class in 1st approach

3 Comments

Well then there is some issue in your setting. To start with check web.xml to see what servlet you have registered Spring's or Cfx's . Refer this for more in detail baeldung.com/apache-cxf-with-spring
I'm using spring boot and in that using cxf dependency
In that case use cxf-spring-boot-starter-jaxws dependency and remove spring-boot-starter-web. Refer this cxf.apache.org/docs/springboot.html
0

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

1 Comment

If i'm using cxf then what is the need to add jersey dependency
0

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.

4 Comments

not able to get it plz explain .why should i have jersey config if i'm using cxf dependency
Can you please share your Pom or gradle ??
kj007 i have added my Pom file
Did you check my updated answer about CXF configuration, do you have that and still facing issue after removing @RestController

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.