以爲程式都寫完了,應該進度超快
結果這邊不滿意,那邊不高興
這裏套件版本升一下,那邊寫法改一下
結果居然整個進度大落後!!!
窩的老天!!!
那麼就手起刀落開始今天的進度吧!!
沒錯,本次直接懶到一個極致,只給restful協定,其他就都不給了
更加體會何爲restful搜尋方式
//restful 必備annotation BJ4
@RestController
public class UserInfoController {
//引入Service 請愛用Resource 依據官方寫法寫Autowired會被同事討厭的
@Resource
private UserInfoService userInfoService;
//Post request method
@PostMapping
public ResponseEntity createProfile(@RequestBody UserInfo userInfo) throws Exception {
//隨便傳個創建吧
return new ResponseEntity(userInfoService.createDoc(userInfo), HttpStatus.CREATED);
}
}
這裏我們簡單塞個UUID給他就好了,並且注入DAO
@Service
public class UserInfoService {
@Resource
private UserInfoDAO userInfoDAO;
public String createDoc(UserInfo userInfo) throws IOException {
//除非人品異常爆發,不然不會重複的ID生成
UUID uuid = UUID.randomUUID();
//完整填充Bean後放入資料庫
userInfo.setId(uuid.toString());
return userInfoDAO.createDoc(userInfo).getResult().name();
}
}
//這裡記得用static Enum還要點來點去的,有點麻煩
import static com.ithelp.ironman.ecdemo.constant.EsConstant.INDEX;
import static com.ithelp.ironman.ecdemo.constant.EsConstant.TYPE;
@Repository
public class UserInfoDAO {
@Resource
private RestHighLevelClient client;
@Resource
private ObjectMapper objectMapper;
public IndexResponse createDoc(UserInfo userInfo) throws IOException {
IndexRequest indexRequest = new IndexRequest(INDEX);
indexRequest.id(userInfo.getId());
indexRequest.source(convertUserInfoToMap(userInfo));
return client.index(indexRequest, RequestOptions.DEFAULT);
}
private Map<String, Object> convertUserInfoToMap(UserInfo userInfo) {
return objectMapper.convertValue(userInfo, Map.class);
}
}
來檢查下 yml 是否正確,先從本機測試
elasticsearch:
host: localhost
port: 9200
username:
password:
#elasticsearch:
# host: 3586ff6792224f91ad8d0d99ef387c44.asia-east1.gcp.elastic-cloud.com
# port: 9243
# username: elastic
# password: vOtc8JDpeqxhiGQrfk2NZQJI
server:
port: 8081
最後一樣的Do Re Mi Sol~
直接post打一打吧
# elasticsearch:
# host: localhost
# port: 9200
# username:
# password:
elasticsearch:
host: 3586ff6792224f91ad8d0d99ef387c44.asia-east1.gcp.elastic-cloud.com
port: 9243
username: elastic
password: vOtc8JDpeqxhiGQrfk2NZQJI
server:
port: 8081
反正坑都踩過填過了,小事麻
來回去加班囉~