springboot使用jQuery调用RESTful Web服务

文章来源:https://spring.io/guides/gs/consuming-rest-jquery/

一、创建一个简单的maven工程spring-boot-jquery

二、在webapp目录下分别添加文件hello.js、index.html,文件内容分别如下:

$(document).ready(function() {
    $.ajax({
        url: "http://rest-service.guides.spring.io/greeting"
    }).then(function(data) {
       $('.greeting-id').append(data.id);
       $('.greeting-content').append(data.content);
    });
});


    
        Hello jQuery
        
        
    

    
        

The ID is

The content is

三、打开postman,查看请求http://rest-service.guides.spring.io/greeting

{
    "id": 2236,
    "content": "Hello, World!"
}

springboot使用jQuery调用RESTful Web服务_第1张图片

四、将spring-boot-jquery添加到tomcat,启动tomcat

四月 19, 2019 7:25:38 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:spring-boot-jquery' did not find a matching property.
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version:        Apache Tomcat/9.0.8
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built:          Apr 27 2018 19:32:00 UTC
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number:         9.0.8.0
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name:               Windows 10
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version:            10.0
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture:          amd64
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home:             H:\JDK\jdk1.8.0_171\jre
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version:           1.8.0_171-b11
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor:            Oracle Corporation
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE:         H:\Tomcat\apache-tomcat-9.0.8
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME:         H:\Tomcat\apache-tomcat-9.0.8
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:57779
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -javaagent:H:\eclipse4.11\configuration\org.eclipse.osgi\409\0\.cp\lib\javaagent-shaded.jar
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=H:\Tomcat\apache-tomcat-9.0.8
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=H:\Tomcat\apache-tomcat-9.0.8
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=H:\Tomcat\apache-tomcat-9.0.8\wtpwebapps
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=H:\Tomcat\apache-tomcat-9.0.8\endorsed
四月 19, 2019 7:25:38 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=GBK
四月 19, 2019 7:25:38 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: Loaded APR based Apache Tomcat Native library [1.2.16] using APR version [1.6.3].
四月 19, 2019 7:25:38 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
四月 19, 2019 7:25:38 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
四月 19, 2019 7:25:39 下午 org.apache.catalina.core.AprLifecycleListener initializeSSL
信息: OpenSSL successfully initialized [OpenSSL 1.0.2m  2 Nov 2017]
四月 19, 2019 7:25:39 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8088"]
四月 19, 2019 7:25:40 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
四月 19, 2019 7:25:40 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-nio-8009"]
四月 19, 2019 7:25:40 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
四月 19, 2019 7:25:40 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 3339 ms
四月 19, 2019 7:25:40 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Catalina]
四月 19, 2019 7:25:40 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/9.0.8
四月 19, 2019 7:25:41 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 7:25:41 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\docs]
四月 19, 2019 7:25:41 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 7:25:41 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\docs] has finished in [100] ms
四月 19, 2019 7:25:41 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\examples]
四月 19, 2019 7:25:41 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 7:25:41 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
四月 19, 2019 7:25:41 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
四月 19, 2019 7:25:41 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: attributeAdded('StockTicker', 'async.Stockticker@3f363cf5')
四月 19, 2019 7:25:41 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\examples] has finished in [595] ms
四月 19, 2019 7:25:41 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\host-manager]
四月 19, 2019 7:25:41 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 7:25:41 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\host-manager] has finished in [142] ms
四月 19, 2019 7:25:41 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\manager]
四月 19, 2019 7:25:42 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 7:25:42 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\manager] has finished in [84] ms
四月 19, 2019 7:25:42 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\ROOT]
四月 19, 2019 7:25:42 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
四月 19, 2019 7:25:42 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory [H:\Tomcat\apache-tomcat-9.0.8\webapps\ROOT] has finished in [93] ms
四月 19, 2019 7:25:42 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8088"]
四月 19, 2019 7:25:42 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
四月 19, 2019 7:25:42 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 1907 ms

五、打开浏览器,打开http://localhost:8088/spring-boot-jquery/index.html,查看页面内容,显示如下:

springboot使用jQuery调用RESTful Web服务_第2张图片

从以上结果可以看出调用 RESTful Web服务成功

六、项目目录结构如下:

springboot使用jQuery调用RESTful Web服务_第3张图片

你可能感兴趣的:(springboot使用jQuery调用RESTful Web服务)