带有 spring 引导的 apache cxf 不工作但是 spring 带有其他注释的引导工作?

apache cxf with spring boot not working but spring boot with other annotations working?

spring boot main class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
@ComponentScan(basePackages = { "com.example.demo" })
@EnableAutoConfiguration
public class CrudApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrudApplication.class, args);
    }

}

rest api's class with cxf annotations not working

@Path("/main")
public class ControllerClass {
       @GET
       @Path("/cxf")
       public String addcsf(@RequestParam String name) {

            Emp emp = new Emp();
            emp.setName(name);


            empDao.save(emp);

            String ret = "CXFFFFFFFF, user name = " + name;

            return ret;

        }

}

but working without cxf

@Controller
public class ControllerClass {
    @Autowired
    EmpDao empDao;

       @GetMapping(path = "/add")
        @ResponseBody
        public String addUser(@RequestParam String name) {

            Emp emp = new Emp();
            emp.setName(name);


            empDao.save(emp);

            String ret = "User account has been added, user name = " + name;

            return ret;

        }
}

adding pom.xml in which i added apache cxf

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>crud</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <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-security</artifactId>
        </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency> -->
                <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

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

</project>

could you please tell me what i'm missing in this getting below error when cxf is added Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

3 月 15 日,星期五 16:10:05 IST 2019 出现意外错误(类型=未找到,状态=404)。 没有可用的消息 这是我的 application.propertiesfile

cxf.path=/services
cxf.jaxrs.client.headers.accept=text/plain
cxf.jaxrs.client.classes-scan-packages=com.example.demo
# MySQL jdbc connection url.
spring.datasource.url=jdbc:mysql://localhost:3306/restapi
# MySQL jdbc driver class name.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# MySQL database username and password
spring.datasource.username=root
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

下面给出了适合我的例子。

测试urlhttp://localhost:8080/services/main/cxf

application.properties

cxf.path=/services
cxf.jaxrs.client.headers.accept=text/plain
cxf.jaxrs.client.classes-scan-packages=com.example.cxfboot

控制器class

package com.example.cxfboot;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.springframework.web.bind.annotation.RequestParam;

@Path("/main")
public class ControllerClass {
    @GET
    @Path("/cxf")
    public String addcsf(@RequestParam String name) {

        System.out.println("Ghanta");

        return "BC";

    }

}

配置class.

import org.apache.cxf.Bus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class CxfBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(CxfBootApplication.class, args);
    }

    @Autowired
    private Bus bus;

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

和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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.19.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>cxf-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cxf-boot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.2.5</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>