【问题标题】:apache cxf with spring boot not working but spring boot with other annotations working?带有spring boot的apache cxf无法正常工作,但带有其他注释的spring boot可以正常工作?
【发布时间】:2019-08-06 10:26:01
【问题描述】:

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 IST 出现意外错误(类型=未找到,状态=404)。 没有可用的消息 这是我的 application.properties 文件

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

【问题讨论】:

    标签: java apache spring-boot cxf


    【解决方案1】:

    下面给出的例子对我有用。

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

    【讨论】:

    • ...尝试但在启动应用程序本身时出现问题
    • 春靴版本?
    • versionf- 2.1.3.
    • java.lang.IllegalStateException: org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration.mbeanExporter 上的错误处理条件
    • 使用我的 sql 加载驱动程序类的问题
    猜你喜欢
    • 2018-08-25
    • 2018-06-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2020-02-22
    • 2016-06-25
    • 2015-01-31
    相关资源
    最近更新 更多