本文介绍了REST服务出现以下错误:资源不可用Glassfish 4.0 JAX-RS 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Glassfish 4.0上部署一个简单的JAX-RS服务,并不断出现以下错误:

I am trying to deploy a simple JAX-RS service on Glassfish 4.0 and keep getting the following error:

HTTP Status 404 - Not Found
type Status report
messageNot Found
descriptionThe requested resource is not available.
GlassFish Server Open Source Edition 4.0

War文件可以在Glassfish服务器中很好地部署,但是看来类加载器没有完成其工作,并且没有适当地公开其余服务.我试图弄清楚为什么类无法正确加载.我知道这可能是一个简单的配置更改,但是我找不到它.

War file deploys fine in Glassfish server however it appears the class loader is not doing its job and exposing the rest service appropriately. I am trying to figure out why class is not loading appropriately. I know it is probably a simple configuration change however I have not been able to find it.

配置:glassfish-web.xml:

Configuration:glassfish-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
  <context-root>/reports</context-root>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>
</glassfish-web-app>

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">
    <servlet>
        <servlet-name>Jersey</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

REST服务代码:

package com.esa.report.rest.service;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.core.MediaType;

@Path("weeklyStatusReport")
@RequestScoped
public class WeeklyStatusReportService {

    @Context
    private UriInfo context;

    public WeeklyStatusReportService() {
    }

    @GET
    @Path("run/{esaId}")
    @Produces({MediaType.APPLICATION_XHTML_XML})
    public String runReport(@PathParam("esaId") String esaId){
        return("Hello esaId: "+esaId);
    }

    @GET
    @Produces("text/html")
    public String getHtml() {
        return("hello this is the weekly status report");
    }

    @PUT
    @Consumes("text/html")
    public void putHtml(String content) {
    }
}

战争使用/reports的根上下文进行部署,我使用的url是:

The war is deployed with the root context of /reports and the url I am using is:

http://localhost:8080/reports/rest/weeklyStatusReport/run/123

推荐答案

首先,丢弃您在web.xml中编写的所有内容.在GlassFish(和所有JavaEE 7容器)上,JAX-RS可以直接使用,不需要进行配置.

First of all, discard everything you wrote in web.xml. On GlassFish (and all JavaEE 7 containers) JAX-RS works out of the box, no configuration needed.

然后,您必须在类路径中有一个javax.ws.rs.core.Application子类,声明一个@ApplicationPath("/")(这告诉容器启动JAX-RS引擎).

Then you must have in your classpath a javax.ws.rs.core.Application subclass, declaring an @ApplicationPath("/") (this tells the container to start the JAX-RS engine).

Application Server将自动提取其他资源.

The other resources will be picked up automatically by the Application Server.`

这篇关于REST服务出现以下错误:资源不可用Glassfish 4.0 JAX-RS 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:01