创建配置MAVEN,CXF和spring的示例应用程序

时间:2018-06-14 16:48:16

标签: 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 个答案:

答案 0 :(得分:0)

1。)将@Produces(&#34; text / palin&#34;)更改为@Produces(&#34; text / plain&#34;)

2。)在@Path(&#34; / {method}&#34;)中,您使用了单词method来表示路径参数的标识符。因此,改变一些明智的名称。例如,将@Path(&#34; / {method}&#34;)更改为@Path(&#34; / {name}&#34;)以及@PathParam(&#34;方法&#34;)到@PathParam(&#34;名称&#34;)(否则可能会让你感到困惑:)

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