Glassfish服务器无法找到资源简单JAX-RS Hello World

发布于 2025-02-03 06:44:58 字数 5776 浏览 4 评论 0原文

刚刚使用JAX-RS制作了一个简单的REST API,并使用Maven进行构建/包装。

代码结构如下所示:

.
├── pom.xml
├── src
│   └── main
│       └── java
│           ├── Hello.java
│           └── WEB-INF
│               └── web.xml
└── target
    ├── classes
    │   └── com
    │       └── satyam
    │           └── Hello.class
    ├── generated-sources
    │   └── annotations
    ├── maven-archiver
    │   └── pom.properties
    ├── maven-status
    │   └── maven-compiler-plugin
    │       └── compile
    │           └── default-compile
    │               ├── createdFiles.lst
    │               └── inputFiles.lst
    ├── satyam
    │   ├── META-INF
    │   └── WEB-INF
    │       ├── classes
    │       │   └── com
    │       │       └── satyam
    │       │           └── Hello.class
    │       └── lib
    │           ├── activation-1.1.jar
    │           ├── javaee-api-8.0.1.jar
    │           └── javax.mail-1.6.2.jar
    └── satyam.war


我的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>

  <groupId>com.satyam</groupId>
  <artifactId>satyam</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>mypackage</name>

  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>satyam</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.1.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.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</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是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

我的hello.java是:

package com.satyam;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("hello")
public class Hello {

    @GET
    @Produces("text/plain")
    public String sayHello()
    {
        return "Hello, World!";
    }
}

当我尝试打开时: http:// localhost:8080/satyam/rest/hello 或者 http:// localhost:8080/rest/hello

y hile fe Irror:http status 404-找不到有关

我如何创建项目和我修改的事物的更多信息:

  • 使用maven-arven ighetype创建的项目:maven-arketype-webapp //如陈述在Maven网站中,
  • re-name src/main/webApp to src/main/java,否则我的java文件并没有
  • 在pom.xml rest中添加javax自己的依赖性。
  • 我已经从命令行启动了Glassfish Server: /opt/glassfish4/bin/asadmin start-domain - verbose
  • 和我正在部署战争文件为:/opt/opt/glassfish4/bin/bin/asadmin deploy/home/satyam/satyam/proj/proj/proj/target/satyam.war

Just made a simple REST API using jax-rs and used maven for build/packaging.

Code structure is as below post compilation:

.
├── pom.xml
├── src
│   └── main
│       └── java
│           ├── Hello.java
│           └── WEB-INF
│               └── web.xml
└── target
    ├── classes
    │   └── com
    │       └── satyam
    │           └── Hello.class
    ├── generated-sources
    │   └── annotations
    ├── maven-archiver
    │   └── pom.properties
    ├── maven-status
    │   └── maven-compiler-plugin
    │       └── compile
    │           └── default-compile
    │               ├── createdFiles.lst
    │               └── inputFiles.lst
    ├── satyam
    │   ├── META-INF
    │   └── WEB-INF
    │       ├── classes
    │       │   └── com
    │       │       └── satyam
    │       │           └── Hello.class
    │       └── lib
    │           ├── activation-1.1.jar
    │           ├── javaee-api-8.0.1.jar
    │           └── javax.mail-1.6.2.jar
    └── satyam.war


My pom.xml is:

<?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>com.satyam</groupId>
  <artifactId>satyam</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>mypackage</name>

  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>satyam</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.1.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.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</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>

My web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

My Hello.java is:

package com.satyam;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("hello")
public class Hello {

    @GET
    @Produces("text/plain")
    public String sayHello()
    {
        return "Hello, World!";
    }
}

When I try to open:
http://localhost:8080/satyam/rest/hello
or
http://localhost:8080/rest/hello

I get error: HTTP Status 404 - Not Found

Some more info about how I created the project and things I modified:

  • Project created using maven archetype: maven-archetype-webapp // as stated in maven website
  • Re-name src/main/webapp to src/main/java otherwise my java files were not compiling
  • Added only dependency for javax myself in pom.xml rest is as it is.
  • I have started glassfish server from command line as:
    /opt/glassfish4/bin/asadmin start-domain --verbose
  • And I am deploying my war file as: /opt/glassfish4/bin/asadmin deploy /home/satyam/proj/target/satyam.war

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

优雅的叶子 2025-02-10 06:44:58

不确定这是否是问题,但是Web.xml中的URL模式对我来说似乎很奇怪。我会更改

&lt; url-pattern&gt;/rest/*&lt;/url-pattern&gt;

&lt; url-pattern&gt;/rest&lt;/url-pattern&gt;

如果您仍然遇到问题,请尝试通过直接访问Admin面板和日志来查看Glassfish本身具有什么。我建议连接到端口4848(管理控制台)并在那里检查您的映射。

http:// localhost:4848
(用户:管理员,通过:adminAdmin)

也可以在域日志中查看。它们应该在此目录中:

cd $GLASSFISH/domains

您应该在这里看到文件结构,例如{domainname}/logs。

Not sure if this is the issue but the url pattern in the web.xml seems strange to me. I would change

<url-pattern>/rest/*</url-pattern>
to
<url-pattern>/rest</url-pattern>

If you still have issues try to see what Glassfish itself has by directly accessing admin panel and logs. I would suggest connecting to port 4848 (admin console) and checking your mappings there.

http://localhost:4848
(user: admin, pass: adminadmin)

Also you can take a look at in the domain logs. They should be in this directory:

cd $GLASSFISH/domains

There you should see file structure like {domainName}/logs.

决绝 2025-02-10 06:44:58

通过查看Glassfish服务器应用程序部分,我发现了工作的Web应用程序与非工作JAX-RS Web应用程序之间的区别。

基本上是模块 javax.ws.rs.core.application 在非工作场景中缺少。正如我在评论部分中所说的那样。感谢@yordan Boev建议研究管理面板。

后来提取了 *.war文件,发现Web.xml根本不存在。

为了解决问题,我必须修改目录结构如下:

早些时候,当事物不起作用的目录结构如下:

.
├── pom.xml
├── src
│   └── main
│       └── java
│           ├── Hello.java
│           └── WEB-INF
│               └── web.xml

现在为了使事情起作用,我必须在“ WebApp”目录中放置“ Web-Inf”目录,我必须将“ WebApp”目录带入一个级别,即它应该存在于“主”目录下,如下所示:

.
├── pom.xml
├── src
│   └── main
│       ├── java
│       │   └── Hello.java
│       └── webapp
│           └── WEB-INF
│               └── web.xml

By looking at the glassfish server application section I found the difference between a working web app and a non working jax-rs web app.

Basically Module javax.ws.rs.core.Application was missing in non working scenario. As I have stated in a comment section. Thanks @Yordan Boev for suggesting to look into admin panel.

Later extracted the *.war file and found that web.xml is not at all present in it.

In order to fix that I have to modify the directory structure as below:

Earlier when things were not working directory structure was as below:

.
├── pom.xml
├── src
│   └── main
│       └── java
│           ├── Hello.java
│           └── WEB-INF
│               └── web.xml

Now in order to make things work I had to put "WEB-INF" inside "webapp" directory and I have to bring "webapp" directory one level up i.e. it should be present under "main" directory as shown below:

.
├── pom.xml
├── src
│   └── main
│       ├── java
│       │   └── Hello.java
│       └── webapp
│           └── WEB-INF
│               └── web.xml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文