简单的JavaEE HTML GET / POST应用程序 [英] Simple JavaEE HTML GET/POST application

查看:109
本文介绍了简单的JavaEE HTML GET / POST应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用JavaEE(我在JavaSE中表现得非常流利),并且无法将我的大脑包装在即使是最简单的应用程序所需的所有新事物中。现在我正尝试使用Glassfish 4在IntelliJ中使用JAX-RS注释来生成一个简单的Hello Worldhtml页面。我已经搜索了这些注释的正确使用方法,并且看起来好像我正确地做了它,但无论我在本地主机中导航的位置如何,我都会收到404。我开始认为我的代码中缺少重要的组件,而我对JavaEE知之甚少,无法知道我错过了什么(可能它可能是web xml文件中的某些东西,我不知道太多了)。这是我写的代码,减去进口:

I'm just starting out with JavaEE (I'm decently fluent in JavaSE) and am having trouble wrapping my brain around all the new things that are required to make even the simplest of applications. Right now I am trying to use JAX-RS annotations to generate a simple "Hello World" html page in IntelliJ using Glassfish 4. I've searched around for the proper use of these annotations, and it seems like I'm doing it correctly, but I keep getting a 404 no matter where I navigate in localhost. I'm starting to think I'm missing vital components in my code, and I don't know enough about JavaEE to know what I'm missing (perhaps it could be something in the web xml file, which I don't know too much about). Here is the code that I wrote, minus the imports:

@LocalBean
@Stateless
@Path("/hello")
@Produces("text/html")
public class Hello {

    @GET
    @Path("/world")
    public String printHelloWorld() {
        return "<html lang=\"en\"><body><h1>Hello, World!</h1></body></html>";
    }
}

服务器本身启动并运行,应用程序似乎正确部署。在启动时设置的默认URL为 http:// localhost:8080 / HelloWorld_war_exploded /
所以我的理解是我应该去
http:// localhost:8080 / HelloWorld_war_exploded / hello / world
来显示消息。

The server itself is up and running and the application seems to deploy correctly. The default URL that is set up at launch time is "http://localhost:8080/HelloWorld_war_exploded/", so my understanding is that I should be going to http://localhost:8080/HelloWorld_war_exploded/hello/world to display the message.

如果有人能指出我的方向是正确的,那就太好了。谢谢!

If anybody could point me in the right direction that would be great. Thanks!

编辑:这是我的XML文件,我根本没有改变:

Here is my XML file, which I did not change at all:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

查看Lutz的评论后,我调查了基本网址问题,目前正在查看以下链接(无法链接两次以上):hxxp://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/twbs_jaxrs_configwebxml.html?cp = SSAW57_8.5.5%2F1-3-0-28-2-0-1

Upon looking at Lutz's comment, I've investigated the base URL issue and am currently viewing the following link (can't link more than twice with my points): hxxp://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/twbs_jaxrs_configwebxml.html?cp=SSAW57_8.5.5%2F1-3-0-28-2-0-1

我会相应更新。感谢您的帮助!

I will update accordingly. Thanks for the help!

推荐答案

您需要在web.xml中配置Jersey(Glassfish中的JAX-RS实现)。您目前只有JSF配置

You need to configure Jersey (the JAX-RS implementation in Glassfish) in your web.xml. You currently only have JSF configuration

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>the.package.where.your.resources.are</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

< url-mapping> 是您的泽西岛应用程序的基础。所以你可以使用

The <url-mapping> is the base for your Jersey app. So you would use

http://localhost:8080/HelloWorld_war_exploded/api/hello/world

如果您想使用标准JAX-RS配置,您可以执行

If you want to use Standard JAX-RS configuration, you can do

<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>

<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

这将扫描您的整个类路径以获取资源,因此您不需要指定包以前的配置。

This will scan your entire classpath for resources, so you don't need to specify the package, like the previous configuration.

或者您可以使用Java代码

Or you can use Java code

@javax.ws.rs.ApplicationPath("/api")
public class RestApplication extends javax.ws.rs.core.Application {

}

让这个类为空也将扫描您的资源的整个类路径。或者您可以显式添加您的类

Leaving this class empty will also scan the entire classpath for your resources. Or you can add your classes explicitly

@javax.ws.rs.ApplicationPath("/api")
public class RestApplication extends javax.ws.rs.core.Application {
     @Override
     public Set<Class<?>> getClasess() {
         Set<Class<?>> classes = new HashSet<>();
         classes.add(Hello.class);
         return classes;
     }
}

这篇关于简单的JavaEE HTML GET / POST应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆