blob: 9d506ae7973046da0d56cea0898496fd1d1591d0 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en_US" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>REST over Servlet ยท GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="../../gitbook/style.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="../styles/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="rest-over-vertx.html" />
<link rel="prev" href="transport.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" >
<span>
just-for-gitbook-build
</span>
<ul class="articles">
<li class="chapter " data-level="1.1.1" data-path="verticle-count.html">
<a href="verticle-count.html">
hidden
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2" data-path="../">
<a href="../">
Overview
</a>
</li>
<li class="chapter " data-level="1.3" data-path="../catalog/start.html">
<a href="../catalog/start.html">
Getting Started
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../start/terminology.html">
<a href="../start/terminology.html">
Glossary
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../start/architecture.html">
<a href="../start/architecture.html">
Architecture
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../start/development-environment.html">
<a href="../start/development-environment.html">
Development environment
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="../start/first-sample.html">
<a href="../start/first-sample.html">
Develop the first microservice
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../catalog/build-provider.html">
<a href="../catalog/build-provider.html">
Development Service Provider
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../build-provider/definition/service-definition.html">
<a href="../build-provider/definition/service-definition.html">
Service definition
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../build-provider/define-contract.html">
<a href="../build-provider/define-contract.html">
Service contract definition
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../build-provider/code-first.html">
<a href="../build-provider/code-first.html">
Implicit API definition
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="../build-provider/swagger-annotation.html">
<a href="../build-provider/swagger-annotation.html">
Use Swagger annotations
</a>
</li>
<li class="chapter " data-level="1.4.5" data-path="../build-provider/springmvc.html">
<a href="../build-provider/springmvc.html">
Develop with SpringMVC
</a>
</li>
<li class="chapter " data-level="1.4.6" data-path="../build-provider/jaxrs.html">
<a href="../build-provider/jaxrs.html">
Develop with JAX-RS
</a>
</li>
<li class="chapter " data-level="1.4.7" data-path="../build-provider/transparent-rpc.html">
<a href="../build-provider/transparent-rpc.html">
Develop with Transparent RPC
</a>
</li>
<li class="chapter " data-level="1.4.8" data-path="../build-provider/interface-constraints.html">
<a href="../build-provider/interface-constraints.html">
Interface definition and data type
</a>
</li>
<li class="chapter " data-level="1.4.9" data-path="../build-provider/listen-address-and-publish-address.html">
<a href="../build-provider/listen-address-and-publish-address.html">
Service listening address and publishing address
</a>
</li>
<li class="chapter " data-level="1.4.10" data-path="../build-provider/thread-pool.html">
<a href="../build-provider/thread-pool.html">
Thread pool
</a>
</li>
<li class="chapter " data-level="1.4.11" data-path="../build-provider/service-configuration.html">
<a href="../build-provider/service-configuration.html">
Service Configuration
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.11.1" data-path="../build-provider/configuration/lb-strategy.html">
<a href="../build-provider/configuration/lb-strategy.html">
Load Balancing Policy
</a>
</li>
<li class="chapter " data-level="1.4.11.2" data-path="../build-provider/configuration/ratelimite-strategy.html">
<a href="../build-provider/configuration/ratelimite-strategy.html">
Rate Limiting Policy
</a>
</li>
<li class="chapter " data-level="1.4.11.3" data-path="../build-provider/configuration/downgrade-strategy.html">
<a href="../build-provider/configuration/downgrade-strategy.html">
Fallback Policy
</a>
</li>
<li class="chapter " data-level="1.4.11.4" data-path="../build-provider/configuration/parameter-validator.html">
<a href="../build-provider/configuration/parameter-validator.html">
Parameter Validator
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.12" data-path="../build-provider/bootup.html">
<a href="../build-provider/bootup.html">
Boot-up Process
</a>
</li>
<li class="chapter " data-level="1.4.13" data-path="../build-provider/access-log-configuration.html">
<a href="../build-provider/access-log-configuration.html">
Access Log Configuration
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../catalog/build-consumer.html">
<a href="../catalog/build-consumer.html">
Writing Service Consumer
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../build-consumer/common-configuration.html">
<a href="../build-consumer/common-configuration.html">
Consumer common configuration
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../build-consumer/using-resttemplate.html">
<a href="../build-consumer/using-resttemplate.html">
Using Rest Template
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../build-consumer/using-AsyncRestTemplate.html">
<a href="../build-consumer/using-AsyncRestTemplate.html">
Using AsynRestTemplate
</a>
</li>
<li class="chapter " data-level="1.5.4" data-path="../build-consumer/develop-consumer-using-rpc.html">
<a href="../build-consumer/develop-consumer-using-rpc.html">
Using with RPC
</a>
</li>
<li class="chapter " data-level="1.5.5" data-path="../build-consumer/with-contract.html">
<a href="../build-consumer/with-contract.html">
Contract
</a>
</li>
<li class="chapter " data-level="1.5.6" data-path="../build-consumer/invoke-control.html">
<a href="../build-consumer/invoke-control.html">
Invoke control
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.6.1" data-path="../build-consumer/circuit-breaker.html">
<a href="../build-consumer/circuit-breaker.html">
Circuit Breaker
</a>
</li>
<li class="chapter " data-level="1.5.6.2" data-path="../build-consumer/flow-control.html">
<a href="../build-consumer/flow-control.html">
Flow Control
</a>
</li>
<li class="chapter " data-level="1.5.6.3" data-path="../build-consumer/fault-injection.html">
<a href="../build-consumer/fault-injection.html">
Fault Injection
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.7" data-path="../build-consumer/3rd-party-service-invoke.html">
<a href="../build-consumer/3rd-party-service-invoke.html">
Invoke 3rd-party REST services
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="transport.html">
<a href="transport.html">
Transports
</a>
<ul class="articles">
<li class="chapter active" data-level="1.6.1" data-path="rest-over-servlet.html">
<a href="rest-over-servlet.html">
REST over Servlet
</a>
</li>
<li class="chapter " data-level="1.6.2" data-path="rest-over-vertx.html">
<a href="rest-over-vertx.html">
REST over Vertx
</a>
</li>
<li class="chapter " data-level="1.6.3" data-path="highway-rpc.html">
<a href="highway-rpc.html">
Highway
</a>
</li>
<li class="chapter " data-level="1.6.4" data-path="http2.html">
<a href="http2.html">
HTTP2
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.7" data-path="../catalog/general-develop.html">
<a href="../catalog/general-develop.html">
General Development
</a>
<ul class="articles">
<li class="chapter " data-level="1.7.1" data-path="../general-development/visit-sc.html">
<a href="../general-development/visit-sc.html">
Access Service Center
</a>
</li>
<li class="chapter " data-level="1.7.2" data-path="../general-development/metrics.html">
<a href="../general-development/metrics.html">
Metrics
</a>
</li>
<li class="chapter " data-level="1.7.3" data-path="../general-development/microservice-invocation-chain.html">
<a href="../general-development/microservice-invocation-chain.html">
Microservice invocation chain
</a>
</li>
<li class="chapter " data-level="1.7.4" data-path="../general-development/customized-tracing.html">
<a href="../general-development/customized-tracing.html">
Customized-Tracing
</a>
</li>
<li class="chapter " data-level="1.7.5" data-path="../general-development/local-develop-test.html">
<a href="../general-development/local-develop-test.html">
Local development and testing
</a>
</li>
<li class="chapter " data-level="1.7.6" data-path="../general-development/http-filter.html">
<a href="../general-development/http-filter.html">
Http Filter
</a>
</li>
<li class="chapter " data-level="1.7.7" data-path="../general-development/file-upload.html">
<a href="../general-development/file-upload.html">
File Uploading
</a>
</li>
<li class="chapter " data-level="1.7.8" data-path="../general-development/file-download.html">
<a href="../general-development/file-download.html">
File Downloading
</a>
</li>
<li class="chapter " data-level="1.7.9" data-path="../general-development/reactive.html">
<a href="../general-development/reactive.html">
Reactive Programing
</a>
</li>
<li class="chapter " data-level="1.7.10" data-path="../general-development/dnsconfig.html">
<a href="../general-development/dnsconfig.html">
DNS Custom Configuration
</a>
</li>
<li class="chapter " data-level="1.7.11" data-path="../general-development/dai-li-she-zhi.html">
<a href="../general-development/dai-li-she-zhi.html">
Proxy Settings
</a>
</li>
<li class="chapter " data-level="1.7.12" data-path="../general-development/report-framework-version.html">
<a href="../general-development/report-framework-version.html">
Report framework version
</a>
</li>
<li class="chapter " data-level="1.7.13" data-path="../general-development/cross-app-invocation.html">
<a href="../general-development/cross-app-invocation.html">
Cross-application invocation
</a>
</li>
<li class="chapter " data-level="1.7.14" data-path="../general-development/secret-field.html">
<a href="../general-development/secret-field.html">
Customized serialization and deserialization
</a>
</li>
<li class="chapter " data-level="1.7.15" data-path="../general-development/context.html">
<a href="../general-development/context.html">
Using Context to pass control messages
</a>
</li>
<li class="chapter " data-level="1.7.16" data-path="../general-development/produceprocess.html">
<a href="../general-development/produceprocess.html">
Return value serialization extension
</a>
</li>
<li class="chapter " data-level="1.7.17" data-path="../general-development/CORS.html">
<a href="../general-development/CORS.html">
CORS mechanism
</a>
</li>
<li class="chapter " data-level="1.7.18" data-path="../general-development/AlarmEvent.html">
<a href="../general-development/AlarmEvent.html">
Get fuse and instance isolation alarm event information
</a>
</li>
<li class="chapter " data-level="1.7.19" data-path="../general-development/shutdown.html">
<a href="../general-development/shutdown.html">
Shutdown gracefully
</a>
</li>
<li class="chapter " data-level="1.7.20" data-path="../general-development/error-handling.html">
<a href="../general-development/error-handling.html">
Handling exceptions
</a>
</li>
<li class="chapter " data-level="1.7.21" data-path="../general-development/multienvironment.html">
<a href="../general-development/multienvironment.html">
Multi-environment isolation between microservice instances
</a>
</li>
<li class="chapter " data-level="1.7.22" data-path="../general-development/thread-model.html">
<a href="../general-development/thread-model.html">
Thread Model
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.8" data-path="../catalog/config.html">
<a href="../catalog/config.html">
Configuration
</a>
<ul class="articles">
<li class="chapter " data-level="1.8.1" data-path="../config/general-config.html">
<a href="../config/general-config.html">
General config
</a>
</li>
<li class="chapter " data-level="1.8.2" data-path="../config/inject-config.html">
<a href="../config/inject-config.html">
Configuration injection
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.9" data-path="../edge/open-service.html">
<a href="../edge/open-service.html">
Service Capability Open
</a>
<ul class="articles">
<li class="chapter " data-level="1.9.1" data-path="../edge/by-servicecomb-sdk.html">
<a href="../edge/by-servicecomb-sdk.html">
Using Edge Service
</a>
</li>
<li class="chapter " data-level="1.9.2" data-path="../edge/nginx.html">
<a href="../edge/nginx.html">
Using confd and Nginx as edge services
</a>
</li>
<li class="chapter " data-level="1.9.3" data-path="../edge/zuul.html">
<a href="../edge/zuul.html">
Use zuul as edge services
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.10" data-path="../catalog/service-package-run.html">
<a href="../catalog/service-package-run.html">
Service Packing and Running
</a>
<ul class="articles">
<li class="chapter " data-level="1.10.1" data-path="../packaging/standalone.html">
<a href="../packaging/standalone.html">
Standalone mode
</a>
</li>
<li class="chapter " data-level="1.10.2" data-path="../packaging/web-container.html">
<a href="../packaging/web-container.html">
WEB container mode
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.11" data-path="../catalog/security.html">
<a href="../catalog/security.html">
Micro Service Security
</a>
<ul class="articles">
<li class="chapter " data-level="1.11.1" data-path="../security/tls.html">
<a href="../security/tls.html">
Using TLS
</a>
</li>
<li class="chapter " data-level="1.11.2" data-path="../security/rsa.html">
<a href="../security/rsa.html">
Using RSA certification
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.12" data-path="../using-java-chassis-in-spring-boot.html">
<a href="../using-java-chassis-in-spring-boot.html">
Using java chassis in Spring Boot
</a>
<ul class="articles">
<li class="chapter " data-level="1.12.1" data-path="../using-java-chassis-in-spring-boot/components-for-spring-boot.html">
<a href="../using-java-chassis-in-spring-boot/components-for-spring-boot.html">
spring boot starter for java-chassis
</a>
</li>
<li class="chapter " data-level="1.12.2" data-path="../using-java-chassis-in-spring-boot/java-application.html">
<a href="../using-java-chassis-in-spring-boot/java-application.html">
JAVA application development
</a>
</li>
<li class="chapter " data-level="1.12.3" data-path="../using-java-chassis-in-spring-boot/web-application.html">
<a href="../using-java-chassis-in-spring-boot/web-application.html">
Web development method development
</a>
</li>
<li class="chapter " data-level="1.12.4" data-path="../using-java-chassis-in-spring-boot/diff-between-java-web.html">
<a href="../using-java-chassis-in-spring-boot/diff-between-java-web.html">
The difference between JAVA application method and Web development method
</a>
</li>
<li class="chapter " data-level="1.12.5" data-path="../using-java-chassis-in-spring-boot/diff-spring-mvc.html">
<a href="../using-java-chassis-in-spring-boot/diff-spring-mvc.html">
The difference in Spring MVC mode
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.13" data-path="../references-handlers/intruduction.html">
<a href="../references-handlers/intruduction.html">
Handlers reference
</a>
<ul class="articles">
<li class="chapter " data-level="1.13.1" data-path="../references-handlers/loadbalance.html">
<a href="../references-handlers/loadbalance.html">
Load Balancing
</a>
</li>
<li class="chapter " data-level="1.13.2" data-path="../references-handlers/publickey.html">
<a href="../references-handlers/publickey.html">
Public key authentication
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.14" data-path="../question-and-answer/question_answer.html">
<a href="../question-and-answer/question_answer.html">
FAQ
</a>
<ul class="articles">
<li class="chapter " data-level="1.14.1" data-path="../question-and-answer/interface-compatibility.html">
<a href="../question-and-answer/interface-compatibility.html">
Micro Service Interface Compatibility FAQ
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >REST over Servlet</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="rest-over-servlet">REST over Servlet</h1>
<p>The REST over Servlet mode applications runs in web container. You need to create a new servlet project to wrap the microservices, pack them into war packages, and load them into the web container to run.</p>
<h2 id="path-for-external-access">Path for external access</h2>
<p>Not like running as a standalone process, when the microservice runs in the web container, the web root and servlet url pattern will be different.</p>
<p>For the traditional development framework, the consumer needs to perceive the complete url of the service; for example, the web root is /mywebapp, the url pattern is /rest, and the business-level path is /application, then consumer must access the service via the url /mywebapp/rest/application.</p>
<p>So when the deployment pattern changes, like from web container to a standalone process, the consumer or producer have to modify the code to adapt to the changes.</p>
<p>It is recommended to use ServiceComb&apos;s deployment decoupling feature. Whether it is a consumer or a producer, the application don&apos;t perceive the web root and url pattern in the code, while ServiceComb will automatically adapt them for the producer instance at runtime.</p>
<p>For some legacy systems, if users expect to use restTemplate.getForObject(&quot;cse://serviceName/mywebapp/rest/application&quot;...) without too many changes, then the path of the interface should be defined as /mywebapp/rest/application:</p>
<pre><code>@RestSchema(schemaId = &quot;test&quot;)
@RequestMapping(path = &quot;/mywebapp/rest/application&quot;)
</code></pre><p>However, it is still recommended to use a deployment-independent way to write the code, which introduces less code modifications when the deployment pattern changes.</p>
<h2 id="maven-dependencies">maven dependencies</h2>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.apache.servicecomb<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>transport-rest-servlet<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<h2 id="configurations">Configurations</h2>
<p>When integrating with servlet, there are a few concepts involved:</p>
<ul>
<li><p>Start spring context<br>Note the following startup methods cannot be used at the same time, just choose one of them. </p>
<ul>
<li>Without SpringMVC UI or RestController</li>
</ul>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span>
<span class="hljs-attr">version</span>=<span class="hljs-string">&quot;3.0&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">context-param</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>contextConfigLocation<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>classpath*:META-INF/spring/*.bean.xml<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">context-param</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">listener</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">listener-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.RestServletContextListener<span class="hljs-tag">&lt;/<span class="hljs-name">listener-class</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">listener</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span>
</code></pre>
<p>The <code>classpath*:META-INF/spring/*.bean.xml</code> configured in contextConfigLocation is optional, because the ServiceComb will ensure that it is included in the load path.</p>
<p>This is just an example to indicate that the user can customize the contextConfigLocation.</p>
<ul>
<li>Use SpringMVC UI or RestController, and org.apache.servicecomb.transport.rest.servlet.CseDispatcherServlet exists</li>
</ul>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span>
<span class="hljs-attr">version</span>=<span class="hljs-string">&quot;3.0&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>SpringMVCServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.CseDispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>SpringMVCServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>yourUrlPattern<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span>
</code></pre>
<p><strong>Note</strong><br>This servlet is not the processing entry of ServiceComb, but the processing entry of UI or RestController.</p>
<ul>
<li><p>Use SpringMVC&apos;s UI or RestController, and there is no org.apache.servicecomb.transport.rest.servlet.CseDispatcherServlet</p>
<p>In this case, the application class should inherit SpringMVC&apos;s DispatcherServlet, and then configure its implementation classes in CseDispatcherServlet&apos;s way.</p>
</li>
</ul>
<pre><code>@Override
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent){
setContextClass(CseXmlWebApplicationContext.class);
return super.createWebApplicationContext(parent);
}
</code></pre></li>
<li><p>ServiceComb servlet </p>
<p>The url pattern can be set according to the business logic. The following <code>/rest/*</code> is just an example, not a fixed value.</p>
<p>Url pattern must end with <code>/*</code></p>
<p>The following two declarations types can not be used at the same time.</p>
<ul>
<li>Standard declaration</li>
</ul>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span>
<span class="hljs-attr">version</span>=<span class="hljs-string">&quot;3.0&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>RestServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.RestServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">async-supported</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">async-supported</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>RestServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/rest/*<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span>
</code></pre>
<ul>
<li>Quick declaration </li>
</ul>
<pre><code class="lang-yaml">servicecomb.rest.servlet.urlPattern: /rest/*
</code></pre>
<p>Specify urlPattern in the microservice.yaml file. When ServiceComb starts, it will automatically create RestServlet and set the corresponding urlPattern.</p>
</li>
</ul>
<h2 id="configuration-example-for-typical-scenarios">Configuration example for typical scenarios</h2>
<ul>
<li><p>Standard declaration in pure ServiceComb mode</p>
<p>web.xml: </p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span>
<span class="hljs-attr">version</span>=<span class="hljs-string">&quot;3.0&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">listener</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">listener-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.RestServletContextListener<span class="hljs-tag">&lt;/<span class="hljs-name">listener-class</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">listener</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>RestServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.RestServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">async-supported</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">async-supported</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>RestServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/rest/*<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span>
</code></pre>
</li>
<li><p>Quick declaration in pure ServiceComb mode
web.xml&#xFF1A; </p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span>
<span class="hljs-attr">version</span>=<span class="hljs-string">&quot;3.0&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">listener</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">listener-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.RestServletContextListener<span class="hljs-tag">&lt;/<span class="hljs-name">listener-class</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">listener</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span>
</code></pre>
<p>microservice.yaml&#xFF1A;</p>
<pre><code class="lang-yaml">servicecomb.rest.servlet.urlPattern: /rest/*
</code></pre>
</li>
<li><p>SpringMVC or RestController provide web services, ServiceComb proxy the requests as consumer</p>
<p>web.xml&#xFF1A;</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span>
<span class="hljs-attr">version</span>=<span class="hljs-string">&quot;3.0&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>SpringMVCServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.CseDispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>SpringMVCServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>yourUrlPattern<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span>
</code></pre>
<p>microservice.yaml&#xFF1A;
Servicecomb.rest.address and servicecomb.rest.servlet.urlPattern are not configured</p>
</li>
<li><p>SpringMVC UI/RestController and ServiceComb provide services at the same time
web.xml&#xFF1A;</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span>
<span class="hljs-attr">version</span>=<span class="hljs-string">&quot;3.0&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>SpringMVCServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.apache.servicecomb.transport.rest.servlet.CseDispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>SpringMVCServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>yourUrlPattern<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span>
</code></pre>
<p>microservice.yaml&#xFF1A; </p>
<pre><code class="lang-yaml"><span class="hljs-attr">servicecomb:</span>
<span class="hljs-attr"> rest:</span>
<span class="hljs-attr"> servlet:</span>
<span class="hljs-attr"> urlPattern:</span> /rest/*
<span class="hljs-attr"> address:</span> <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">8080</span>
</code></pre>
</li>
</ul>
<h2 id="things-to-notice-with-servlet-filter">Things to notice with servlet filter</h2>
<p>RestServlet works in asynchronous mode. According to the servlet 3.0 standard, the entire work chain must be asynchronous. Therefore, the servlet filter should be set to be asynchronous when it&apos;s added to the chain:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">filter</span>&gt;</span>
......
<span class="hljs-tag">&lt;<span class="hljs-name">async-supported</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">async-supported</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span>
</code></pre>
<h2 id="configuration-items">Configuration items</h2>
<p>The related items for REST over Servlet in the microservice.yaml are described below:</p>
<p>Table1-1 REST over Servlet Configuration Items</p>
<table>
<thead>
<tr>
<th style="text-align:left">Configuration Item</th>
<th style="text-align:left">Default Value</th>
<th style="text-align:left">Required</th>
<th style="text-align:left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">servicecomb.rest.address</td>
<td style="text-align:left">0.0.0.0:8080</td>
<td style="text-align:left">No</td>
<td style="text-align:left">The service listening address<br>Should be the same with the web container&apos;s listening address</td>
</tr>
<tr>
<td style="text-align:left">servicecomb.rest.server.timeout</td>
<td style="text-align:left">-1</td>
<td style="text-align:left">No</td>
<td style="text-align:left">Server aync servlet timeout in milliseconds, suggest set to -1</td>
</tr>
<tr>
<td style="text-align:left">servicecomb.rest.server.requestWaitInPoolTimeout</td>
<td style="text-align:left">30000</td>
<td style="text-align:left">No</td>
<td style="text-align:left">for sync business logic, timeout in milliseconds for waiting in executor queue</td>
</tr>
<tr>
<td style="text-align:left">servicecomb.rest.servlet.urlPattern</td>
<td style="text-align:left"></td>
<td style="text-align:left">No</td>
<td style="text-align:left">Used to simplify servlet+servlet mapping config<br>This item is used only when servlet+servlet mapping is not configured in web.xml.The format is:/* or /path/*, where path can be nested</td>
</tr>
</tbody>
</table>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="transport.html" class="navigation navigation-prev " aria-label="Previous page: Transports">
<i class="fa fa-angle-left"></i>
</a>
<a href="rest-over-vertx.html" class="navigation navigation-next " aria-label="Next page: REST over Vertx">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"REST over Servlet","level":"1.6.1","depth":2,"next":{"title":"REST over Vertx","level":"1.6.2","depth":2,"path":"transports/rest-over-vertx.md","ref":"transports/rest-over-vertx.md","articles":[]},"previous":{"title":"Transports","level":"1.6","depth":1,"path":"transports/transport.md","ref":"transports/transport.md","articles":[{"title":"REST over Servlet","level":"1.6.1","depth":2,"path":"transports/rest-over-servlet.md","ref":"transports/rest-over-servlet.md","articles":[]},{"title":"REST over Vertx","level":"1.6.2","depth":2,"path":"transports/rest-over-vertx.md","ref":"transports/rest-over-vertx.md","articles":[]},{"title":"Highway","level":"1.6.3","depth":2,"path":"transports/highway-rpc.md","ref":"transports/highway-rpc.md","articles":[]},{"title":"HTTP2","level":"1.6.4","depth":2,"path":"transports/http2.md","ref":"transports/http2.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"introduction.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"en_US","gitbook":"*"},"file":{"path":"transports/rest-over-servlet.md","mtime":"2020-02-10T07:31:45.852Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-02-10T07:32:25.443Z"},"basePath":"..","book":{"language":"en_US"}});
});
</script>
</div>
<script src="../../gitbook/gitbook.js"></script>
<script src="../../gitbook/theme.js"></script>
<script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>