Syntax
public java.lang.String toString(int indentFactor) throws JSONException
Example
import org.json.*;
public class JSONPrettyPrintTest {
public static void main(String args[]) throws JSONException {
String json = "{" +
"Name : Jai," +
"Age : 25, " +
"Salary: 25000.00 " +
"}";
JSONObject jsonObj = new JSONObject(json);
System.out.println("Pretty Print of JSON:");
System.out.println(jsonObj.toString(4)); // pretty print json
}
}
Output
Pretty Print of JSON:
{
"Salary": 25000,
"Age": 25,
"Name": "Jai"
}
# Install MariaDB 10.3
yum install rh-mariadb103-mariadb-server rh-mariadb103-mariadb-server-utils -y
# Add MariaDB 10.3 to $PATH
scl enable rh-mariadb103 bash
source /opt/rh/rh-mariadb103/enable
# start 10.3 server
chown -R mysql:mysql /var/opt/rh/rh-mariadb103/lib/mysql
;
systemctl start rh-mariadb103-mariadb
# Upgrade tables
mysql_upgrade -p
[PASSWORD]# Set 10.3 to start on boot
systemctl enable rh-mariadb103-mariadb
# Add 10.3 to paths on reboot (and remove 10.2)
rm /etc/profile.d/rh-mariadb102.sh
cp /opt/rh/rh-mariadb103/enable /etc/profile.d/rh-mariadb103.sh
# increase max connections number
systemctl edit rh-mariadb103-mariadb
[Service]LimitNOFILE=65535
LimitNPROC=65535
vi /etc/opt/rh/rh-mariadb103/my.cnf
[mysqld]max_connections=1000
open_files_limit=65535
# restart mariadb103
systemctl daemon-reload
systemctl restart rh-mariadb103-mariadb
# check result
mysql -e 'show variables like "max_connections"'
https://www.server-world.info/en/note?os=CentOS_7&p=mariadb103&f=4
Update: for production environment, to avoid exposing the password in the command line, since you can query the processes with ps
, previous commands with history
, etc etc. You could:
- Create a script like this:
touch setEnv.sh
- Edit
setEnv.sh
to export the JASYPT_ENCRYPTOR_PASSWORD
variable#!/bin/bash
export JASYPT_ENCRYPTOR_PASSWORD=supersecretz
- Execute the file with
. setEnv.sh
- Run the app in background with
mvn spring-boot:run &
- Delete the file
setEnv.sh
- Unset the previous environment variable with:
unset JASYPT_ENCRYPTOR_PASSWORD
https://stackoverflow.com/questions/37404703/spring-boot-how-to-hide-passwords-in-properties-file
當(dāng)SPRING INTEGRATION的流程中從HTTP outboundGateway轉(zhuǎn)成JmsGateway時(shí),會(huì)報(bào)header的錯(cuò)誤,這時(shí)就要把相關(guān)多余的header移除。
.headerFilter("Api-Key", "Content-Type", "X-Powered-By", "Content-Language", "Transfer-Encoding", "Cache-Control", "Keep-Alive", "Set-Cookie")
https://stackoverflow.com/questions/50608415/cwsia0112e-the-property-name-keep-alive-is-not-a-valid-java-identifier
RestTemplate是Spring提供的用于訪問(wèn)Rest服務(wù)的客戶端,
RestTemplate提供了多種便捷訪問(wèn)遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率。
調(diào)用RestTemplate的默認(rèn)構(gòu)造函數(shù),RestTemplate對(duì)象在底層通過(guò)使用java.net包下的實(shí)現(xiàn)創(chuàng)建HTTP 請(qǐng)求,
可以通過(guò)使用ClientHttpRequestFactory指定不同的HTTP請(qǐng)求方式。
ClientHttpRequestFactory接口主要提供了兩種實(shí)現(xiàn)方式
1、一種是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)創(chuàng)建底層的Http請(qǐng)求連接。
2、一種方式是使用HttpComponentsClientHttpRequestFactory方式,底層使用HttpClient訪問(wèn)遠(yuǎn)程的Http服務(wù),使用HttpClient可以配置連接池和證書等信息。
默認(rèn)的 RestTemplate 有個(gè)機(jī)制是請(qǐng)求狀態(tài)碼非200 就拋出異常,會(huì)中斷接下來(lái)的操作。如果不想中斷對(duì)結(jié)果數(shù)據(jù)得解析,可以通過(guò)覆蓋默認(rèn)的 ResponseErrorHandler ,見下面的示例,示例中的方法中基本都是空方法,只要對(duì)hasError修改下,讓他一直返回true,即是不檢查狀態(tài)碼及拋異常了。
package com.example.demo.web.config;
import java.io.IOException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) throws Exception {
RestTemplate restTemplate = new RestTemplate(factory);
ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
return true;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
}
};
restTemplate.setErrorHandler(responseErrorHandler);
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
//讀取超時(shí)5秒
factory.setReadTimeout(5000);
//連接超時(shí)15秒
factory.setConnectTimeout(15000);
return factory;
}
}
RestTemppate運(yùn)用實(shí)例
package com.example.demo.web.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.example.demo.domain.Book;
@RestController
public class TestBookController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private RestTemplate restTemplate;
@GetMapping("/testaddbook")
public Book testAddBook() {
Book book = new Book();
ResponseEntity<Book> responseEntity = restTemplate.postForEntity( "http://localhost:8061/book", book , Book.class);
return responseEntity.getBody();
}
}
其他方法,catch
HttpStatusCodeException ,
e.getResponseBodyAsString()try {
ResponseEntity<Component> response = restTemplate.exchange(webSvcURL,
HttpMethod.POST,
requestEntity,
Component.class);
} catch (HttpStatusCodeException e) {
List<String> customHeader = e.getResponseHeaders().get("x-app-err-id");
String svcErrorMessageID = "";
if (customHeader != null) {
svcErrorMessageID = customHeader.get(0);
}
throw new CustomException(e.getMessage(), e, svcErrorMessageID);
// You can get the body too but you will have to deserialize it yourself
// e.getResponseBodyAsByteArray()
// e.getResponseBodyAsString()
}
https://stackoverflow.com/questions/7878002/resttemplate-handling-response-headers-body-in-exceptions-restclientexceptionhttps://stackoverflow.com/questions/38093388/spring-resttemplate-exception-handling/51805956#51805956
在SPRING INTEGRATION中,如果exception發(fā)生在各種thread里時(shí),如何將exception返回到指定的channel,之后再繞回到aggrator-channel中。
@Bean
public IntegrationFlow provisionUserFlow() {
return
IntegrationFlows.from("input.channel")
.publishSubscribeChannel(Executors.newCachedThreadPool(),
s -> s.applySequence(true)
.subscribe(f -> f.enrichHeaders(e -> e.header(MessageHeaders.ERROR_CHANNEL, "errorChannel", true))
.handle(provisionerA, "provision")
.channel("aggregatorChannel")
)
.subscribe(f -> f.enrichHeaders(e -> e.header(MessageHeaders.ERROR_CHANNEL, "errorChannel", true))
.handle(provisionerB, "provision")
.channel("aggregatorChannel"))
)
.get();
}
@Bean
public IntegrationFlow aggregateFlow() {
return IntegrationFlows.from("aggregatorChannel")
.channel( aggregatorChannel)
.aggregate( a -> a.processor( collect, "aggregatingMethod"))
.get();
}
@Transformer( inputChannel = "errorChannel", outputChannel = "aggregatorChannel")
public Message<?> errorChannelHandler(ErrorMessage errorMessage) throws RuntimeException {
Message<?> failedMessage = ((MessagingException) errorMessage.getPayload()).getFailedMessage();
Exception exception = (Exception) errorMessage.getPayload();
return MessageBuilder.withPayload( exception.getMessage())
.copyHeadersIfAbsent( failedMessage.getHeaders() )
.build();
}
https://stackoverflow.com/q/46495127/11790720