《Java RESTful Web Service实战》第一章的实现补漏

简介: 韩陆,你好你的书,麻烦写的清楚一点,多写一些,也许还能多点稿费。小韩,写书认真一点。第13页上来就用maven命令行创建项目,这就有问题啊,没有pom.xml文件mvn这个命令怎么跑的起来呢?所以正确的过程是这样的:eclipse上创建项目创建的结果是这样的:

韩陆,你好
你的书,麻烦写的清楚一点,多写一些,也许还能多点稿费。
image
小韩,写书认真一点。
image
第13页上来就用maven命令行创建项目,这就有问题啊,没有pom.xml文件mvn这个命令怎么跑的起来呢?
所以正确的过程是这样的:
eclipse上创建项目
创建的结果是这样的:
image
创建过程,先创建一个maven项目
image
_

然后archetypes选择书中指出的jersey-quickstart-grizzly2
创建项目之后,实际上会报错
默认情况下eclipse创建出来的pom.xml文件中提供的jersey-version的版本是1.20
image
但实际上mavenrepository上提供的最新版也就是1.19.4,所以修改一下
image
正常情况下,创建项目完毕,我们测试一下:
首先当然是参考书中的命令行方式,用curl这个命令去执行,这需要首先下载配置curl
https://curl.haxx.se/windows/
image
image
配置环境变量
image
现在都准备好了我们先看看这个项目的内容,它都自动帮我们生成了什么:
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
image
console中的信息如下所示:
image

我们使用cmd客户端,执行命令行语句:
image
执行结果,和书中写的一样。

目录
相关文章
|
10天前
|
SQL Java
20:基于EL与JSTL的产品管理页-Java Web
20:基于EL与JSTL的产品管理页-Java Web
21 5
|
1天前
|
Java 应用服务中间件 测试技术
深入探索Spring Boot Web应用源码及实战应用
【5月更文挑战第11天】本文将详细解析Spring Boot Web应用的源码架构,并通过一个实际案例,展示如何构建一个基于Spring Boot的Web应用。本文旨在帮助读者更好地理解Spring Boot的内部工作机制,以及如何利用这些机制优化自己的Web应用开发。
8 2
|
1天前
|
JSON Go 数据格式
golang学习7,glang的web的restful接口结构体传参
golang学习7,glang的web的restful接口结构体传参
|
1天前
|
JSON Go 数据格式
golang学习6,glang的web的restful接口传参
golang学习6,glang的web的restful接口传参
|
1天前
|
JSON Go 数据格式
golang学习5,glang的web的restful接口
golang学习5,glang的web的restful接口
|
4天前
|
前端开发 JavaScript Java
Java与Web开发的结合:JSP与Servlet
Java与Web开发的结合:JSP与Servlet
8 0
|
4天前
|
监控 安全 Shell
记一次应急靶场实战--web1
本次应急靶机题目训练主要是为了应对接下来的护网面试和比赛,顺便提高一下自己对应急和溯源的实战能力,这里有两个逗比的事情发生,一是用D盾分析的时候,电脑环境监测到了,把要分析的马杀了,二是,发现无法使用脚本,在自己本机实验,电脑差点搞崩,还好佬在制作时候只是单次运行会占用.切记不要本机实验,一定要在虚拟机中进行测试.
|
9天前
|
XML JSON Java
【Web系列笔记】Restful
本文讨论了RESTful接口设计的原因和原则。传统方式中,http接口常按功能聚合,导致行为不规范,如订单操作有多种请求方式。RESTful设计强调资源的结构清晰、标准统一,通过资源URI、表现层和状态转化来组织接口。它推荐使用GET、POST、PUT和DELETE等HTTP方法对应资源的创建、读取、更新和删除操作。在实践中,应避免URI中包含动词,确保每个URI代表一种资源,并利用HTTP动词表达操作。这样能提高接口的易理解和扩展性。
34 8
|
10天前
|
设计模式 前端开发 Java
19:Web开发模式与MVC设计模式-Java Web
19:Web开发模式与MVC设计模式-Java Web
20 4
|
10天前
|
设计模式 存储 前端开发
18:JavaBean简介及其在表单处理与DAO设计模式中的应用-Java Web
18:JavaBean简介及其在表单处理与DAO设计模式中的应用-Java Web
25 4