Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>demo-spring-boot-convert</artifactId>
<groupId>io.servicecomb.demo</groupId>
<version>0.4.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>demo-spring-boot-springmvc-server-convert</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-transport</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicecomb.demo.springboot.convert.springmvc;

import io.servicecomb.foundation.common.utils.Log4jUtils;
import io.servicecomb.springboot.starter.provider.EnableServiceComb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableServiceComb
public class SpringMvcServerMain {

public static void main(String[] args) throws Exception {
Log4jUtils.init();
SpringApplication.run(SpringMvcServerMain.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.servicecomb.demo.springboot.convert.springmvc.compute;

public interface SpringMvcCompute {

int add(int a, int b);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicecomb.demo.springboot.convert.springmvc.compute;

import io.servicecomb.provider.rest.common.RestSchema;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RestSchema(schemaId = "springMvcCompute")
@Controller
@RequestMapping(path = "/springmvccompute", produces = MediaType.APPLICATION_JSON)
public class SpringMvcComputeImpl implements SpringMvcCompute {

private static final Logger LOGGER = LoggerFactory.getLogger(SpringMvcComputeImpl.class);

@Override
@RequestMapping(path = "/add", method = RequestMethod.GET)
@ResponseBody
public int add(int a, int b) {
LOGGER.info("SpringMvcComputeImpl.add() is called, a = {}, b = {}", a, b);
return a + b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicecomb.demo.springboot.convert.springmvc.handler;

import io.servicecomb.core.Invocation;
import io.servicecomb.core.handler.impl.AbstractHandler;
import io.servicecomb.swagger.invocation.AsyncResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyHandler extends AbstractHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);

@Override
public void handle(Invocation invocation, AsyncResponse asyncResponse) throws Exception {
LOGGER.info("If you see this log, that means this demo project has been converted to ServiceComb framework.");

invocation.next(response -> {
asyncResponse.handle(response);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017 Huawei Technologies Co., Ltd
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

<context:component-scan base-package="io.servicecomb.demo.springboot.convert" />
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017 Huawei Technologies Co., Ltd
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<config>
<handler id="myhandler" class="io.servicecomb.demo.springboot.convert.springmvc.handler.MyHandler"/>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2017 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

paas.logs.dir=target/logs/
paas.logs.file=sample.log

log4j.rootLogger=INFO,paas,stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
APPLICATION_ID: SpringBootConvertDemo
service_description:
name: springmvcServer
version: 0.0.1
cse:
service:
registry:
address: http://127.0.0.1:30100
rest:
address: 0.0.0.0:8080
handler:
chain:
Provider:
default: myhandler
servicecomb:
rest:
servlet:
urlPattern: /springmvccompute/*
19 changes: 19 additions & 0 deletions demo/demo-spring-boot-convert/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>demo-parent</artifactId>
<groupId>io.servicecomb.demo</groupId>
<version>0.4.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>demo-spring-boot-convert</artifactId>
<packaging>pom</packaging>
<modules>
<module>demo-spring-boot-springmvc-server-convert</module>
</modules>


</project>
1 change: 1 addition & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<module>demo-multiple</module>
<module>demo-signature</module>
<module>demo-edge</module>
<module>demo-spring-boot-convert</module>
</modules>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import com.netflix.config.DynamicStringProperty;

public final class ServletConfig {

static final long DEFAULT_TIMEOUT = 3000;

static final String KEY_SERVLET_URL_PATTERN = "servicecomb.rest.servlet.urlPattern";
static final String DEFAULT_URL_PATTERN = "/*";

private ServletConfig() {
}
Expand All @@ -42,7 +44,7 @@ public static String getLocalServerAddress() {

public static String getServletUrlPattern() {
DynamicStringProperty address =
DynamicPropertyFactory.getInstance().getStringProperty(KEY_SERVLET_URL_PATTERN, null);
DynamicPropertyFactory.getInstance().getStringProperty(KEY_SERVLET_URL_PATTERN, DEFAULT_URL_PATTERN);
return address.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;

public class TestServletConfig {

@BeforeClass
public static void classSetup() {
ArchaiusUtils.resetConfig();
Expand All @@ -51,7 +52,23 @@ public void testGetServerTimeout() {
public void testGetServletUrlPattern() {
DynamicPropertyFactory.getInstance();
Configuration configuration = (Configuration) DynamicPropertyFactory.getBackingConfigurationSource();
configuration.setProperty(ServletConfig.KEY_SERVLET_URL_PATTERN, "/*");
// to distinguish this case from testGetServletUrlPatternOnUrlPatternIsNotSet
String urlPattern = "/urlPattern";
configuration.setProperty(ServletConfig.KEY_SERVLET_URL_PATTERN, urlPattern);
Assert.assertEquals(urlPattern, ServletConfig.getServletUrlPattern());
}

@Test
public void testGetServletUrlPatternOnUrlPatternIsNotSet() {
DynamicPropertyFactory.getInstance();
Configuration configuration = (Configuration) DynamicPropertyFactory.getBackingConfigurationSource();
Object preservedProperty = configuration.getProperty(ServletConfig.KEY_SERVLET_URL_PATTERN);
// ensure this property is null
configuration.clearProperty(ServletConfig.KEY_SERVLET_URL_PATTERN);
Assert.assertEquals("/*", ServletConfig.getServletUrlPattern());

if (null != preservedProperty) {
configuration.setProperty(ServletConfig.KEY_SERVLET_URL_PATTERN, preservedProperty);
}
}
}