改變spring boot的預設端口 https://www.baeldung.com/spring-boot-change-port
方式一:
server:
port : 8081
方式二:
SpringApplication app = new SpringApplication(Application.class);
app.setDefaultProperties(Collections.singletonMap("server.port", "8083"));
app.run(args);
方式三:
@Component
public class ServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8086);
}
}
mongo,spring 連接配接參數
spring:
data:
mongodb:
uri: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
如
spring:
data:
mongodb:
uri: mongodb://admin:[email protected]:20000,192.168.31.2:20000,192.168.31.3:20000/testdb?authSource=admin&connect=replicaSet&readPreference=secondaryPreferred&safe=true&authMechanism=SCRAM-SHA-1&maxPoolSize=500&minPoolSize=10=
對mongo字段值進行限制的
https://howtodoinjava.com/hibernate/hibernate-validator-java-bean-validation/
摘抄于https://blog.csdn.net/truong/article/details/28097837
若用戶端通過POST方法,在http body中傳遞的參數為key=value表單形式,則可用@RequestParam進行解析;若傳遞的參數為JSON形式,則使用@RequestBody注解進行解析。
@RequestParam
A) 常用來處理簡單類型的綁定,通過Request.getParameter() 擷取的String可直接轉換為簡單類型的情況( String–> 簡單類型的轉換操作由ConversionService配置的轉換器來完成);因為使用request.getParameter()方式擷取參數,是以可以處理get 方式中queryString的值,也可以處理post方式中 body data的值;
B)用來處理Content-Type: 為 application/x-www-form-urlencoded編碼的内容,送出方式GET、POST;
C) 該注解有兩個屬性: value、required; value用來指定要傳入值的id名稱,required用來訓示參數是否必須綁定;
@RequestBody
該注解常用來處理Content-Type: 不是application/x-www-form-urlencoded編碼的内容,例如application/json, application/xml等;
它是通過使用HandlerAdapter 配置的HttpMessageConverters來解析post data body,然後綁定到相應的bean上的。
因為配置有FormHttpMessageConverter,是以也可以用來處理 application/x-www-form-urlencoded的内容,處理完的結果放在一個MultiValueMap<String, String>裡,這種情況在某些特殊需求下使用,詳情檢視FormHttpMessageConverter api;
@RequestHeader、@CookieValue
@SessionAttributes, @ModelAttribute
@PathVariable
fastjson 對objectID JSON.toJSONString轉出來的結果是:
"id":{
"counter":8931467,
"date":1548926486000,
"machineIdentifier":4849263,
"processIdentifier":6629,
"time":1548926486000,
"timeSecond":1548926486,
"timestamp":1548926486
}
解決方式一:
public class ObjectIdSerializer implements ObjectSerializer {
public final static ObjectIdSerializer instance = new ObjectIdSerializer();
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.out;
if (object instanceof ObjectId) {
ObjectId objectId = (ObjectId) object;
out.writeString(objectId.toString());
return;
}
}
}
SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
serializeConfig.put(ObjectId.class , new ObjectIdSerializer());
JSON.toJSONString(map , serializeConfig);
另外還有方法:自定義類實作ResponseBodyAdvice接口,重寫beforeBodyWrite方法,自定義序列化方法,可以參考Java對MongoDB的ObjectId的序列化問題
對時間對處理
JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss.SSS")
alibaba/fastjson