java – 使用WildFly 8的简单REST API

前端之家收集整理的这篇文章主要介绍了java – 使用WildFly 8的简单REST API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,我是这个环境的新手.我以前开发过 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:

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.test.recapp.rest</groupId>
  4. <artifactId>RECAPP-API</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <packaging>war</packaging>
  7.  
  8. <build>
  9. <plugins>
  10. <plugin>
  11. <groupId>org.apache.maven.plugins</groupId>
  12. <artifactId>maven-compiler-plugin</artifactId>
  13. <version>3.1</version>
  14. <configuration>
  15. <source>1.7</source>
  16. <target>1.7</target>
  17. </configuration>
  18. </plugin>
  19. </plugins>
  20. </build>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.jboss.resteasy</groupId>
  24. <artifactId>resteasy-jaxrs</artifactId>
  25. <version>3.0.6.Final</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.jboss.resteasy</groupId>
  29. <artifactId>resteasy-jackson-provider</artifactId>
  30. <version>3.0.6.Final</version>
  31. </dependency>
  32. </dependencies>
  33. </project>

我的JSONService.java:

  1. import javax.ws.rs.GET;
  2. import javax.ws.rs.Path;
  3. import javax.ws.rs.PathParam;
  4. import javax.ws.rs.Produces;
  5. import javax.ws.rs.core.Response;
  6.  
  7. @Path("/message")
  8. public class JSONService {
  9.  
  10. @GET
  11. @Path("/{param}")
  12. @Produces("application/json")
  13. public Response printMessage(@PathParam("param") String msg) {
  14. String result = "Restful example: " + msg;
  15. return Response.status(200).entity(result).build();
  16. }
  17.  
  18. }

最后,我的web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns="http://java.sun.com/xml/ns/javaee"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. id="WebApp_ID" version="3.0">
  7. <display-name>RECAPP-API</display-name>
  8.  
  9. <context-param>
  10. <param-name>resteasy.scan</param-name>
  11. <param-value>true</param-value>
  12. </context-param>
  13.  
  14. <context-param>
  15. <param-name>resteasy.servlet.mapping.prefix</param-name>
  16. <param-value>/rest</param-value>
  17. </context-param>
  18.  
  19. <listener>
  20. <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
  21. </listener>
  22.  
  23. <servlet>
  24. <servlet-name>resteasy-servlet</servlet-name>
  25. <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  26. </servlet>
  27.  
  28. <servlet-mapping>
  29. <servlet-name>resteasy-servlet</servlet-name>
  30. <url-pattern>/rest/*</url-pattern>
  31. </servlet-mapping>
  32.  
  33.  
  34. </web-app>

谢谢你的帮助.

解决方法

快速启动的最佳方法是使用此依赖项.
  1. <dependency>
  2. <groupId>javax</groupId>
  3. <artifactId>javaee-api</artifactId>
  4. <version>7.0</version>
  5. <scope>provided</scope>
  6. </dependency>

添加一个扩展应用程序类的类

  1. @ApplicationPath("rest")
  2. public class ConfigApp extends Application {
  3. public ConfigApp(){
  4. }
  5. }

而已.没有web.xml更改(仅限不需要web.xml).

并使用host:port /< warname> / rest /< endpoint path>访问您的休息端点

猜你在找的Java相关文章