apache cxf弹簧启动无法正常工作,但弹簧启动与其他注释工作?

问题描述 投票:0回答:1

春季靴子主类

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

}

休息api的类与cxf注释无法正常工作

@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时,你能告诉我在下面的错误中我错过了什么吗?Whitelabel错误页面这个应用程序没有明确的/错误映射,所以你看到这是一个后备。

Fri Mar 15 16:10:05 IST 2019出现意外错误(type = Not Found,status = 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个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.