Java 带有 WildFly 8 的简单 REST API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22502824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple REST API with WildFly 8
提问by Honus Wagner
First off, I am new to this environment. I've developed Java before, but not for an application server. Having never done that, I've never worked with JBoss or WildFly previously.
首先,我是这个环境的新手。我以前开发过 Java,但不是为应用服务器开发的。从来没有这样做过,我以前从未与 JBoss 或 WildFly 合作过。
I've been able to set up and run the WildFly server, and access it at 127.0.0.1:9990
. When I deploy my .war
file, the server doesn't react and I can't access the URLs.
我已经能够设置和运行 WildFly 服务器,并在127.0.0.1:9990
. 当我部署我的.war
文件时,服务器没有反应,我无法访问 URL。
The WildFly server does state that my deployment succeeded and is active, then I try to access: 127.0.0.1:8080/RECAPP-API/rest/message/test
and I get a 404 (Page not found error).
WildFly 服务器确实声明我的部署成功并且处于活动状态,然后我尝试访问:127.0.0.1:8080/RECAPP-API/rest/message/test
并收到 404(未找到页面错误)。
I'm using Maven, so first, my pom.xml
:
我正在使用 Maven,所以首先,我的pom.xml
:
<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.test.recapp.rest</groupId>
<artifactId>RECAPP-API</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-Hymanson-provider</artifactId>
<version>3.0.6.Final</version>
</dependency>
</dependencies>
</project>
My JSONService.java
:
我的JSONService.java
:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/message")
public class JSONService {
@GET
@Path("/{param}")
@Produces("application/json")
public Response printMessage(@PathParam("param") String msg) {
String result = "Restful example: " + msg;
return Response.status(200).entity(result).build();
}
}
And finally, my web.xml
:
最后,我的web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>RECAPP-API</display-name>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Thanks for your help.
谢谢你的帮助。
回答by whyceewhite
Your example looks correct.
你的例子看起来是正确的。
It helps to restart your jboss server and redeploy your WAR to rule out potential caching.
它有助于重新启动您的 jboss 服务器并重新部署您的 WAR 以排除潜在的缓存。
Also, your web.xml
could be shortened to use javax.ws.rs.core.Application
as shown below.
此外,您web.xml
可以缩短使用javax.ws.rs.core.Application
,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>RECAPP-API</display-name>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
回答by K.Nicholas
In a Wildfly Quickstartthey seem to prefer using a JaxRsActivator class: Add this to configure your REST service.
在Wildfly 快速入门中,他们似乎更喜欢使用 JaxRsActivator 类:添加它来配置您的 REST 服务。
/**
* A class extending {@link Application} and annotated with @ApplicationPath is the Java EE 6 "no XML" approach to activating
* JAX-RS.
*
* <p>
* Resources are served relative to the servlet path specified in the {@link ApplicationPath} annotation.
* </p>
*/
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
/* class body intentionally left blank */
}
As the comments state, this is a non-xml approach.
正如评论所述,这是一种非 xml 方法。
回答by Shettyh
Best way to quick start is use this dependency .
快速入门的最佳方法是使用此依赖项。
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
And add a class that Extends Application Class
并添加一个扩展应用程序类的类
@ApplicationPath("rest")
public class ConfigApp extends Application {
public ConfigApp(){
}
}
Thats it. No web.xml changes (web.xml is not required only).
就是这样。没有 web.xml 更改(仅不需要 web.xml)。
And access your rest endpoint using host:port/<warname>/rest/<endpoint path>
并使用访问您的休息端点 host:port/<warname>/rest/<endpoint path>