《Java RESTful Web Service实战》第一章的实现补漏
韩陆,你好
你的书,麻烦写的清楚一点,多写一些,也许还能多点稿费。
小韩,写书认真一点。
第13页上来就用maven命令行创建项目,这就有问题啊,没有pom.xml文件mvn这个命令怎么跑的起来呢?
所以正确的过程是这样的:
eclipse上创建项目
创建的结果是这样的:
创建过程,先创建一个maven项目
然后archetypes选择书中指出的jersey-quickstart-grizzly2
创建项目之后,实际上会报错
默认情况下eclipse创建出来的pom.xml文件中提供的jersey-version的版本是1.20
但实际上mavenrepository上提供的最新版也就是1.19.4,所以修改一下
正常情况下,创建项目完毕,我们测试一下:
首先当然是参考书中的命令行方式,用curl这个命令去执行,这需要首先下载配置curl
https://curl.haxx.se/windows/
配置环境变量
现在都准备好了我们先看看这个项目的内容,它都自动帮我们生成了什么:
MyResource.java
package my.restfulwork.jerseyone;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
// The Java class will be hosted at the URI path "/myresource"
@Path("/myresource")
public class MyResource {
// TODO: update the class to suit your needs
// The Java method will process HTTP GET requests
@GET
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String getIt() {
return "Got it!";
}
}
Main.java
package my.restfulwork.jerseyone;
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
public class Main {
private static int getPort(int defaultPort) {
//grab port from environment, otherwise fall back to default port 9998
String httpPort = System.getProperty("jersey.test.port");
if (null != httpPort) {
try {
return Integer.parseInt(httpPort);
} catch (NumberFormatException e) {
}
}
return defaultPort;
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(getPort(9998)).build();
}
public static final URI BASE_URI = getBaseURI();
protected static HttpServer startServer() throws IOException {
ResourceConfig resourceConfig = new PackagesResourceConfig("my.restfulwork.jerseyone");
System.out.println("Starting grizzly2...");
return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
}
public static void main(String[] args) throws IOException {
// Grizzly 2 initialization
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...",
BASE_URI));
System.in.read();
httpServer.stop();
}
}
MainTest.java
package my.restfulwork.jerseyone;
import org.glassfish.grizzly.http.server.HttpServer;
import com.sun.jersey.core.header.MediaTypes;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import junit.framework.TestCase;
public class MainTest extends TestCase {
private HttpServer httpServer;
private WebResource r;
public MainTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
//start the Grizzly2 web container
httpServer = Main.startServer();
// create the client
Client c = Client.create();
r = c.resource(Main.BASE_URI);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
httpServer.stop();
}
/**
* Test to see that the message "Got it!" is sent in the response.
*/
public void testMyResource() {
String responseMsg = r.path("myresource").get(String.class);
assertEquals("Got it!", responseMsg);
}
/**
* Test if a WADL document is available at the relative path
* "application.wadl".
*/
public void testApplicationWadl() {
String serviceWadl = r.path("application.wadl").
accept(MediaTypes.WADL).get(String.class);
assertTrue(serviceWadl.length() > 0);
}
}
上述三个文件是项目自己生成的java代码
我们运行Main.java
console中的信息如下所示:
我们使用cmd客户端,执行命令行语句:
执行结果,和书中写的一样。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
php strtr() 用法
<?php $str = "test"; $str1 = strtr($str, 't', 'z'); // zesz $str2 = strtr($str, 'tt', 'z1');// 1es1 $str3 = strtr($str, 't', '');// test $str4 = strtr($str, 'ts', '12');// 1e21 $str5 = strtr($str, array("t" => '')); // es $str6 = strtr($str, array("e" => 'www', "s" => "hhh"));// twwwhhht
-
下一篇
Coding-and-Paper-Letter(十八)
资源整理。这一期Coding内容有点多,所以将论文单独拆成另一篇。 Coding: 1.利用ggplot2做轨迹方向可视化,从仓库名称看,应该是某次组会的报告。 Spatio temporal R BGU group meeting Sep 2018 2.R语言包gstat,比较出名的地统计学的R包,可以提供IDW,多种Kriging的空间插值。 gstat 3.百度研究院的开源项目NCRF,利用神经网络和条件随机场预测了癌症转移。 NCRF 4.Keras实现深度卷积生成对抗网络的开源项目。 keras dcgan 5.PyTorch交互式深色着色的重新实现。 colorization pytorch 6.Obejective-C上的简单库,用于检测运动类型(步行,跑步,汽车)和计算用户步数。 SOMotionDetector 7.RotationNet将对象的多视图图像作为输入,并联合估计其姿势和对象类别。 rotationnet 8.R语言包rethinking,这个R包与关于贝叶斯数据分析的课程和书籍配套(McElreath 2016.Dational Rethinking.C...
相关文章
文章评论
共有0条评论来说两句吧...