Fixed error codes and added logging (#138)
fixed error codes (400 not 500) and added logging for json Co-authored-by: augustyd <kainytsugua.kerad@gmail.com> Reviewed-on: #138 Co-authored-by: Dariusz Augustyniak <augustyd@noreply.example.com> Co-committed-by: Dariusz Augustyniak <augustyd@noreply.example.com>
This commit is contained in:
		| @@ -32,12 +32,16 @@ public class SparkApplication { | ||||
|             .setPrettyPrinting() | ||||
|             .create(); | ||||
|  | ||||
|         Gson jsongson = new GsonBuilder() | ||||
|             .disableHtmlEscaping() | ||||
|             .create(); | ||||
|  | ||||
|         RestControllerRegistry registry = new RestControllerRegistry(); | ||||
|         registry.registerController(new ProcessorInfoController(logger)); | ||||
|         registry.registerController(new XsdController(gson, logger)); | ||||
|         registry.registerController(new XPathController(gson, logger)); | ||||
|         registry.registerController(new XsltController(gson, logger)); | ||||
|         registry.registerController(new JsonController()); | ||||
|         registry.registerController(new JsonController(gson, jsongson, logger)); | ||||
|  | ||||
|         registry.register(); | ||||
|  | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package com.r11.tools.controller; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import com.google.gson.GsonBuilder; | ||||
| //import com.google.gson.GsonBuilder; | ||||
| import com.google.gson.JsonObject; | ||||
| import com.r11.tools.controller.internal.GlobalControllerManifest; | ||||
| import com.r11.tools.controller.internal.HandlerType; | ||||
| @@ -9,18 +9,22 @@ import com.r11.tools.controller.internal.RestController; | ||||
| import com.r11.tools.controller.internal.ScopedControllerManifest; | ||||
| import spark.Request; | ||||
| import spark.Response; | ||||
| import org.apache.logging.log4j.Logger; | ||||
|  | ||||
| @GlobalControllerManifest(path = "/json") | ||||
| public class JsonController implements RestController { | ||||
|  | ||||
|     private final Gson prettyGson = new GsonBuilder() | ||||
|             .disableHtmlEscaping() | ||||
|             .setPrettyPrinting() | ||||
|             .create(); | ||||
|     private final Logger logger; | ||||
|  | ||||
|     private final Gson gson = new GsonBuilder() | ||||
|             .disableHtmlEscaping() | ||||
|             .create(); | ||||
|     private final Gson prettyGson;  | ||||
|  | ||||
|     private final Gson gson; | ||||
|  | ||||
|     public JsonController(Gson prettyGson, Gson jsongson,Logger logger) { | ||||
|             this.logger = logger; | ||||
|             this.prettyGson = prettyGson; | ||||
|             this.gson = jsongson; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/formatting") | ||||
|     public void formatting(Request request, Response response) { | ||||
| @@ -35,17 +39,20 @@ public class JsonController implements RestController { | ||||
|             responseJson.addProperty("data", this.prettyGson.toJson(requestJson)); | ||||
|             responseJson.addProperty("time", System.currentTimeMillis() - startProcess); | ||||
|  | ||||
|             response.body(this.prettyGson.toJson(responseJson)); | ||||
|              | ||||
|         } catch (Exception e) { | ||||
|             this.logger.error("Error on formatting Json " + e); | ||||
|             Throwable cause = e.getCause(); | ||||
|  | ||||
|             response.status(500); | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage()); | ||||
|             responseJson.addProperty("time", System.currentTimeMillis() - startProcess); | ||||
|  | ||||
|              | ||||
|         }    | ||||
|         this.logger.info("Json processed in " + responseJson.get("time").toString() + " ms."); | ||||
|             response.body(this.prettyGson.toJson(responseJson)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/minimize") | ||||
| @@ -63,14 +70,16 @@ public class JsonController implements RestController { | ||||
|  | ||||
|             response.body(this.gson.toJson(responseJson)); | ||||
|         } catch (Exception e) { | ||||
|             this.logger.error("Error on minimizeing Json " + e); | ||||
|             Throwable cause = e.getCause(); | ||||
|  | ||||
|             response.status(500); | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("data", cause == null ? e.getMessage() : cause.getMessage()); | ||||
|             responseJson.addProperty("time", System.currentTimeMillis() - startProcess); | ||||
|  | ||||
|             response.body(this.prettyGson.toJson(responseJson)); | ||||
|         } | ||||
|         this.logger.info("Json processed in " + responseJson.get("time").toString() + " ms."); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user