java - 在 Java 中创建一个简单的 Restful api
问题描述
我正在尝试通过 intellij 创建一个简单的 Restful hello world api。我使用 maven-archetype-quickstart 创建了一个 maven 项目,然后在 pom.xml 中添加了
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.7.Final</version>
<scope>provided</scope>
</dependency>
然后在 src->main->java->webservice 我有两个文件: App.java
package webservice;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
和 BookRestService.java
package webservice;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/book")
public class BookRestService {
@GET
@Produces("text/plain")
public String getBookTitle() {
return "H2G2";
}
}
我正在使用 jBoss-6.4。Jboss 似乎开始给我以下信息:
15:08:01,550 INFO [org.jboss.ws.common.management] (MSC service thread 1-2) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.3.4.Final-redhat-1
15:08:01,824 INFO [org.jboss.as.remoting] (MSC service thread 1-1) JBAS017100: Listening on 127.0.0.1:4447
15:08:01,824 INFO [org.jboss.as.remoting] (MSC service thread 1-7) JBAS017100: Listening on 127.0.0.1:9999
15:08:01,827 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-3) JBAS015012: Started FileSystemDeploymentService for directory /home/symeon/myprojects/applicationserver/jboss-eap-6.4-clean/standalone/deployments
15:08:02,113 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
15:08:02,114 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
15:08:02,115 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss EAP 6.4.0.GA (AS 7.5.0.Final-redhat-21) started in 5345ms - Started 153 of 191 services (57 services are lazy, passive or on-demand)
Connected to server
[2019-01-24 03:08:02,544] Artifact webservice: Artifact is being deployed, please wait...
15:08:02,721 INFO [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015876: Starting deployment of "webservice" (runtime-name: "webservice.war")
15:08:03,236 INFO [org.jboss.web] (ServerService Thread Pool -- 7) JBAS018210: Register web context: /webservice
15:08:03,400 INFO [org.jboss.as.server] (management-handler-thread - 2) JBAS015859: Deployed "webservice" (runtime-name : "webservice.war")
[2019-01-24 03:08:03,428] Artifact webservice: Artifact is deployed successfully
[2019-01-24 03:08:03,428] Artifact webservice: Deploy took 884 milliseconds
我期望的是当我打开浏览器并编写http://localhost:8080/book以查看 H2G2 作为答案时。我也尝试过http://localhost:8080/webservice/book,因为它提到 Web 上下文已注册到 /webservice 但再次没有成功。我收到 404 Not Found 错误。我做错了什么?
解决方案
在你的App.java
你需要定义ApplicationPath
. 像下面的东西。
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/restRoot")
public class App extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> classes = new HashSet<Class<?>>();
classes.add(BookRestService.class);
return classes;
}
}
在此之后,您的 REST url 将变为
http://localhost:8080/webservice/restRoot/book
推荐阅读
- vue.js - 组件的已创建钩子中未定义 mixin 方法
- android - 如何以编程方式在键盘上滚动回收视图?
- wso2 - WSO2 APIM 2.5 部署模式 Pattern2
- c# - 将约会添加到链接到 iCloud 的非默认日历
- php - 后处理返回的对象 Laravel
- css - 将动态 @keyframes 规则绑定到 Vue 的 `:style` 属性
- algorithm - 从给定的移动数字键盘序列中计算所有可能的字符串
- firebase - 限制每个用户的可调用 https firebase 云函数执行?
- swift - macOS - UserDefaults 在 Swift 和终端中是不同的
- javascript - 每次用户进入网站时自动从数组中加载下一张图片