Hon*_*ner 6 java jboss resteasy maven wildfly
首先,我是这个环境的新手.我以前开发过Java,但不是应用程序服务器.从来没有这样做过,我以前从未使用过JBoss或WildFly.
我已经能够设置和运行WildFly服务器,并访问它127.0.0.1:9990
.当我部署我的.war
文件时,服务器没有反应,我无法访问URL.
WildFly服务器确实声明我的部署成功并且处于活动状态,然后我尝试访问:127.0.0.1:8080/RECAPP-API/rest/message/test
并且我得到404(找不到页面错误).
我正在使用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-jackson-provider</artifactId>
<version>3.0.6.Final</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我的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>
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
在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 */
}
Run Code Online (Sandbox Code Playgroud)
正如评论所述,这是一种非 xml 方法。
快速启动的最佳方法是使用此依赖项.
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
并添加一个扩展应用程序类的类
@ApplicationPath("rest")
public class ConfigApp extends Application {
public ConfigApp(){
}
}
Run Code Online (Sandbox Code Playgroud)
而已.没有web.xml更改(仅限不需要web.xml).
并使用访问您的休息端点 host:port/<warname>/rest/<endpoint path>
你的例子看起来是正确的。
它有助于重新启动 jboss 服务器并重新部署 WAR 以排除潜在的缓存。
另外,您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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23041 次 |
最近记录: |