java - 创建配置 MAVEN、CXF 和 spring 的示例应用程序

标签 java spring rest maven cxf

I am trying to create a sample application and call a service configuring maven, CXF and Spring. When I run on Server gives me following error, can anyone assist me with this please. 

我正在尝试创建一个示例应用程序并调用配置 maven、CXF 和 Spring 的服务。当我在服务器上运行时出现以下错误,任何人都可以帮助我解决这个问题。

我正在尝试创建一个示例应用程序并调用配置 maven、CXF 和 Spring 的服务。当我在服务器上运行时出现以下错误,任何人都可以帮助我解决这个问题。

HTTP Status 404 - /sample_maven/rest/sample/method/hi

type Status report

message /sample_maven/rest/sample/method/hi

description The requested resource is not available.


Maven code for the applications. 

<?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>sample_maven</groupId>
    <artifactId>sample_maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <!-- CXF -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>sample_maven</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven 
                defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Web.xml code for the applicaton

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>PAJIT application</display-name>

    <!-- spring -->
    <context-param>
        <param-name>configlocation</param-name>
        <param-value>/WEB-INF/beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- CXF -->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <display-name>Archetype Created Web Application</display-name>
</web-app>


**beans.xml** code for applications


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <context:component:scan base-package="com.sample.proj"/>
    <jaxrs:server id="rest" address="/">
        <jaxrs:serverBeans>
            <ref bean="HelloWorld" />
        </jaxrs:serverBeans>
    </jaxrs:server>
</beans>


**HelloWorldClass** class


package com.sample.proj;

import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.stereotype.Service;
@Service
@Path("/sample")
public class HelloWorld {
        @GET
    @Path("/{method}")
    @Produces("text/palin")
    public String handleRequest(@PathParam("method")String request) {

        //String text = "successfull";
        return "Hello " + request;
    }
    }

最佳答案

1.) 将 @Produces("text/palin") 更改为 @Produces("text/plain")

2.) 在@Path("/{method}") 中,您使用了单词method 来表示路径参数的标识符。因此,将其更改为一些合理的名称。例如,将 @Path("/{method}") 更改为 @Path("/{name}"),并将 @PathParam("method") 更改为 @PathParam("name") (否则可能会让您感到困惑:)

3.) 尝试访问此 url:/sample_maven/rest/sample/prasad (注意:这里 prasad 将是路径参数的值)

关于java - 创建配置 MAVEN、CXF 和 spring 的示例应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50862388/

相关文章:

java - 如何从 Android 服务获取应用程序上下文?

java - Spring 安全: redirect logged user depending on some conditions

ajax - 无法使用 vue.js 和 ajax 将 json 数据发布到 Rest api

json - 角度2 :ERROR Error: InvalidPipeArgument: '' for pipe 'AsyncPipe' and Angular2:ERROR TypeError: Cannot read property 'dispose' of null

java - 找不到符号java计算代码

java - 使用spring条件bean时如何指定单元测试

java - Joincolumn 返回空值

java - 在设置 javax.net.ssl.trustStore 之前加载的默认信任库

validation - JAX RS 泽西模式验证

java - Java 运行时环境 (JRE) 或 Java 开发工具包 (JDK) 必须可用才能运行 Dbeaver。没有Java虚拟机