带有Spring Boot的apache cxf无法正常工作,但带有其他注释的spring boot可以正常工作吗?

时间:2019-03-15 10:49:51

标签: java apache spring-boot cxf

  

spring boot主类

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

}

  

带有cxf批注的REST API的类不起作用

@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;

        }

}
  

但无需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;

        }
}
  

添加pom.xml   我在其中添加了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>
  

能请你告诉我我在这件事中缺少什么吗   添加cxf时出现以下错误   白标错误页面   此应用程序没有针对/ error的显式映射,因此您将其视为后备。

2019年3月15日星期五16:10:05 发生意外错误(类型=未找到,状态= 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

1 个答案:

答案 0 :(得分:0)

在下面的示例中为我工作。

测试网址http://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

控制器类

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";

    }

}

配置类。

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>