下面列出了怎么用org.springframework.boot.configurationprocessor.json.JSONException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void json_parse() throws JSONException {
long offset = 789;
long timestampToUse = 987;
String input = "123,456," + offset;
String output = myEnrichAndReformatFn._makeJson(input, timestampToUse);
log.info("{} -> {}", input, output);
assertThat("output", output, not(nullValue()));
JSONObject jsonObject = new JSONObject(output);
String latitudeExtracted = jsonObject.getString("latitude");
assertThat("latitude", latitudeExtracted, not(nullValue()));
assertThat("latitude", latitudeExtracted, is(equalTo("123")));
String longitudeExtracted = jsonObject.getString("longitude");
assertThat("longitude", longitudeExtracted, not(nullValue()));
assertThat("longitude", longitudeExtracted, is(equalTo("456")));
String timestampExtracted = jsonObject.getString("timestamp");
assertThat("timestamp", timestampExtracted, not(nullValue()));
assertThat("timestamp", timestampExtracted, is(equalTo(""+timestampToUse)));
assertThat("timestampToUse", timestampToUse, not(equalTo(offset)));
}
/**
* Set the status indicator
*
* @param token JWT
* @return HTTP Response
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> post(JwtAuthenticationToken token, @RequestBody String content) throws JSONException {
UserContext userContext = (UserContext) token.getPrincipal(); // Check if user is logged
JSONObject jsonObject = new JSONObject(content);
String value = jsonObject.get("status").toString();
boolean result = this.customIndicatorService.update(value);
if (result) {
return new ResponseEntity<>(HttpStatus.CREATED);
}
return new ResponseEntity<>("This status does not exist.", HttpStatus.BAD_REQUEST);
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() {
SophiaHttpStatus resultEnum = SophiaHttpStatus.SERVER_TIMEOUT;
String data = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code", resultEnum.getCode());
jsonObject.put("msg", resultEnum.getMessage());
jsonObject.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
data = jsonObject.toString();
logger.error(data);
return new ByteArrayInputStream(data.getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() {
SophiaHttpStatus resultEnum = SophiaHttpStatus.SERVER_TIMEOUT;
String data = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code", resultEnum.getCode());
jsonObject.put("msg", resultEnum.getMessage());
jsonObject.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
data = jsonObject.toString();
logger.error(data);
return new ByteArrayInputStream(data.getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() {
SophiaHttpStatus resultEnum = SophiaHttpStatus.SERVER_TIMEOUT;
String data = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("code", resultEnum.getCode());
jsonObject.put("msg", resultEnum.getMessage());
jsonObject.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
data = jsonObject.toString();
logger.error(data);
return new ByteArrayInputStream(data.getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
public static long countDocuments(String indexName, ElasticsearchProperties properties) throws IOException, JSONException {
HttpGet httpRequest = new HttpGet(("http://" + properties.getHost() + ":" + properties.getPort() + "/" + indexName + "/_count"));
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse httpResponse = httpClient.execute(httpRequest);
return new JSONObject(IOUtils.toString(httpResponse.getEntity().getContent(), Charset.defaultCharset())).getLong("count");
}