You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/09/07 01:54:06 UTC
[incubator-servicecomb-java-chassis] branch master updated:
[SCB-895]When json parse fail will not get 400 but 590
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 1bd50e0 [SCB-895]When json parse fail will not get 400 but 590
1bd50e0 is described below
commit 1bd50e0bf193cfb7b00378e6a1c626980e30d6ba
Author: liubao <ba...@huawei.com>
AuthorDate: Thu Sep 6 11:34:08 2018 +0800
[SCB-895]When json parse fail will not get 400 but 590
---
.../rest/codec/param/BodyProcessorCreator.java | 5 +-
.../jaxrs/client/MultiErrorCodeServiceClient.java | 56 ++++++++++++++++++++++
demo/demo-schema/pom.xml | 4 --
.../demo-spring-boot-discovery-server/pom.xml | 6 ---
demo/demo-springmvc/springmvc-client/pom.xml | 5 ++
demo/demo-springmvc/springmvc-server/pom.xml | 5 ++
integration-tests/pom.xml | 6 ---
7 files changed, 69 insertions(+), 18 deletions(-)
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
index e3016a0..18e5bcf 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/BodyProcessorCreator.java
@@ -92,10 +92,11 @@ public class BodyProcessorCreator implements ParamValueProcessorCreator {
.readValue(inputStream, targetType);
} catch (MismatchedInputException e) {
// there is no way to detect InputStream is empty, so have to catch the exception
- if (!isRequired) {
- LOGGER.warn("Mismatched content and required is false, taken as null. Msg=" + e.getMessage());
+ if (!isRequired && e.getMessage().contains("No content to map due to end-of-input")) {
+ LOGGER.info("Empty content and required is false, taken as null");
return null;
}
+ LOGGER.warn("Mismatched content. Msg=" + e.getMessage());
throw e;
}
}
diff --git a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/MultiErrorCodeServiceClient.java b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/MultiErrorCodeServiceClient.java
index 28e30c5..8dd67e6 100644
--- a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/MultiErrorCodeServiceClient.java
+++ b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/MultiErrorCodeServiceClient.java
@@ -29,9 +29,18 @@ import org.apache.servicecomb.demo.multiErrorCode.MultiRequest;
import org.apache.servicecomb.demo.multiErrorCode.MultiResponse200;
import org.apache.servicecomb.demo.multiErrorCode.MultiResponse400;
import org.apache.servicecomb.demo.multiErrorCode.MultiResponse500;
+import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import io.vertx.core.json.Json;
@@ -40,6 +49,8 @@ import io.vertx.core.json.JsonObject;
public class MultiErrorCodeServiceClient {
private static final String SERVER = "cse://jaxrs";
+ private static String serverDirectURL;
+
private static RestTemplate template = RestTemplateBuilder.create();
public static void runTest() {
@@ -52,6 +63,51 @@ public class MultiErrorCodeServiceClient {
testErrorCodeWithHeaderJAXRSUsingRowType();
testNoClientErrorCode();
}
+
+ prepareServerDirectURL();
+ testErrorCodeWrongType();
+ }
+
+ private static void prepareServerDirectURL() {
+ Microservice microservice = RegistryUtils.getMicroservice();
+ MicroserviceInstance microserviceInstance = (MicroserviceInstance) RegistryUtils.getServiceRegistry()
+ .getAppManager()
+ .getOrCreateMicroserviceVersionRule(microservice.getAppId(), "jaxrs", DefinitionConst.VERSION_RULE_ALL)
+ .getVersionedCache()
+ .mapData()
+ .values()
+ .stream()
+ .findFirst()
+ .get();
+ URIEndpointObject edgeAddress = new URIEndpointObject(microserviceInstance.getEndpoints().get(0));
+ serverDirectURL = String.format("http://%s:%d/", edgeAddress.getHostOrIp(), edgeAddress.getPort());
+ }
+
+ private static void testErrorCodeWrongType() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ String body = "{\"message\":\"hello\",\"code\":\"wrongType\"";
+ HttpEntity<String> entity = new HttpEntity<>(body, headers);
+ ResponseEntity<MultiResponse200> result;
+ try {
+ template
+ .postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
+ } catch (HttpClientErrorException e) {
+ TestMgr.check(e.getStatusCode(), 400);
+ TestMgr.check(e.getMessage(), "400 Bad Request");
+ }
+
+ entity = new HttpEntity<>(null, headers);
+ result = template
+ .postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
+ TestMgr.check(result.getStatusCodeValue(), 590);
+
+ body = "{\"message\":\"hello\",\"code\":\"200\"}";
+ entity = new HttpEntity<>(body, headers);
+ result = template
+ .postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
+ TestMgr.check(result.getStatusCode(), 200);
+ TestMgr.check(result.getBody().getMessage(), "success result");
}
private static void testErrorCode() {
diff --git a/demo/demo-schema/pom.xml b/demo/demo-schema/pom.xml
index 4ad7eaa..6c4ad1d 100644
--- a/demo/demo-schema/pom.xml
+++ b/demo/demo-schema/pom.xml
@@ -61,10 +61,6 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>swagger-generator-springmvc</artifactId>
</dependency>
- <dependency>
- <groupId>org.apache.servicecomb.demo</groupId>
- <artifactId>demo-signature</artifactId>
- </dependency>
</dependencies>
<build>
<plugins>
diff --git a/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/pom.xml b/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/pom.xml
index 04a945a..c444198 100644
--- a/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/pom.xml
+++ b/demo/demo-spring-boot-discovery/demo-spring-boot-discovery-server/pom.xml
@@ -30,12 +30,6 @@
<dependency>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-schema</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.apache.servicecomb.demo</groupId>
- <artifactId>demo-signature</artifactId>
- </exclusion>
- </exclusions>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
diff --git a/demo/demo-springmvc/springmvc-client/pom.xml b/demo/demo-springmvc/springmvc-client/pom.xml
index c3c06fd..21be7b0 100644
--- a/demo/demo-springmvc/springmvc-client/pom.xml
+++ b/demo/demo-springmvc/springmvc-client/pom.xml
@@ -49,6 +49,11 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-fault-injection</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-signature</artifactId>
+ </dependency>
</dependencies>
<properties>
diff --git a/demo/demo-springmvc/springmvc-server/pom.xml b/demo/demo-springmvc/springmvc-server/pom.xml
index 8e651dd..5a53fcf 100644
--- a/demo/demo-springmvc/springmvc-server/pom.xml
+++ b/demo/demo-springmvc/springmvc-server/pom.xml
@@ -50,6 +50,11 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>config-cc</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.servicecomb.demo</groupId>
+ <artifactId>demo-signature</artifactId>
+ </dependency>
</dependencies>
<properties>
diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml
index 9ec78a8..b2f69b8 100644
--- a/integration-tests/pom.xml
+++ b/integration-tests/pom.xml
@@ -64,12 +64,6 @@
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-schema</artifactId>
<version>1.1.0-SNAPSHOT</version>
- <exclusions>
- <exclusion>
- <groupId>org.apache.servicecomb.demo</groupId>
- <artifactId>demo-signature</artifactId>
- </exclusion>
- </exclusions>
</dependency>
<dependency>
<groupId>org.apache.servicecomb.tests</groupId>