如何调用休息终点

时间:2016-08-12 09:24:04

标签: java rest maven jersey

我一直在处理我在网上找到的教程 - 但是我遇到了一些问题,因为我不能确定需要使用的URL,所以我可以通过浏览器调用我的休息终点 - 我一直在404错误。

我不确定我是否缺少一些配置或者什么不是,但我的服务器似乎在eclipse中开始正常。请有人提供一些帮助。

由于

休息结束点

@Path("/jersey-hello")
public class JerseyResource
{
    private static final Logger LOGGER = Logger.getLogger(JerseyResource.class.getName());

    @Autowired
    private GreetingService greetingService;

    @Inject
    private DateTimeService timeService;

    public JerseyResource()
    {
        LOGGER.info("HelloWorldResource()");
    }

    @GET
    @Path("hi")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHello()
    {
        return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("world"));
    }
}

注册资源配置

public class MyApplication extends ResourceConfig
{
    public MyApplication()
    {
        register(RequestContextFilter.class);
        register(JerseyResource.class);
        register(SpringSingletonResource.class);
        register(SpringRequestResource.class);
        register(CustomExceptionMapper.class);
    }
}

的web.xml

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

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <module-name>restprj</module-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>SpringApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.rest.test.demo.MyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringApplication</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

应用上下文

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="greetingService"
        class="com.rest.test.demo.GreetingServiceImpl" />

    <bean class="com.rest.test.demo.DateTimeService"
        scope="request" />

    <bean
        class="com.rest.test.demo.SpringSingletonResource" />

    <bean
        class="com.rest.test.demo.SpringRequestResource"
        scope="request" />

    <bean
        class="com.rest.test.demo.CustomExceptionMapper" />
</beans>

的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>
    <version>1.0-SNAPSHOT</version>
    <groupId>com.rest.test</groupId>

    <artifactId>helloworld-spring-webapp</artifactId>
    <packaging>war</packaging>
    <name>jersey-examples-helloworld-spring-webapp</name>

    <description>Spring 3 Integration Jersey Example</description>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.23.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.2.RELEASE</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
            <version>2.23.1</version>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>org.glassfish.jersey.examples.helloworld.jaxrs.App</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>7.6.8.v20121106</version>
                <configuration>
                    <webApp>
                        <descriptor>/src/main/resources/webapp/WEB-INF/web.xml</descriptor>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>xml-maven-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                    </plugin>

                </plugins>
            </build>
        </profile>
    </profiles>

</project>

服务器启动

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.rest.test:helloworld-spring-webapp:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.codehaus.mojo:exec-maven-plugin is missing. @ line 66, column 12
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
Downloading: http://salnexus/nexus/content/groups/salerio/org/codehaus/mojo/exec-maven-plugin/maven-metadata.xml
Downloaded: http://salnexus/nexus/content/groups/salerio/org/codehaus/mojo/exec-maven-plugin/maven-metadata.xml (710 B at 3.8 KB/sec)
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jersey-examples-helloworld-spring-webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:7.6.8.v20121106:run (default-cli) @ helloworld-spring-webapp >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloworld-spring-webapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ helloworld-spring-webapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ helloworld-spring-webapp ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\dev-rl\hg\restprj\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ helloworld-spring-webapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< jetty-maven-plugin:7.6.8.v20121106:run (default-cli) @ helloworld-spring-webapp <<<
[INFO] 
[INFO] --- jetty-maven-plugin:7.6.8.v20121106:run (default-cli) @ helloworld-spring-webapp ---
[INFO] Configuring Jetty for project: jersey-examples-helloworld-spring-webapp
[INFO] webAppSourceDirectory not set. Defaulting to C:\dev-rl\hg\restprj\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\dev-rl\hg\restprj\target\classes
[INFO] Context path = /
[INFO] Tmp directory = C:\dev-rl\hg\restprj\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = /src/main/resources/webapp/WEB-INF/web.xml
[INFO] Webapp directory = C:\dev-rl\hg\restprj\src\main\webapp
2016-08-12 11:18:10.083:INFO:oejs.Server:jetty-7.6.8.v20121106
2016-08-12 11:18:10.603:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2016-08-12 11:18:10.807:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/dev-rl/hg/restprj/src/main/webapp/},file:/C:/dev-rl/hg/restprj/src/main/webapp/
2016-08-12 11:18:10.858:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

1 个答案:

答案 0 :(得分:1)

如果您将所有内容移至预期的默认文件夹,我认为这是最简单的。

src/main/resources/webappsrc/main/webapp的webapp文件夹。

我将application-context.xml重命名为src/webapp/WEB-INF/applicationContext.xml并删除了 web.xml 中的context-param

我在内容中添加了一个简单的index.html&#34;你好&#34;进入src/webapp/index.html

我从 pom.xml 中的jetty插件中删除了配置标记。

之后我做了mvn jetty:run。我能够http://localhost:8080/index.html

相关问题