Files
elasticsearchStudy/src/main/java/top/fjy8018/elasticsearch/Controller/GetBookController.java
2018-05-19 16:56:01 +08:00

46 lines
1.3 KiB
Java

package top.fjy8018.elasticsearch.Controller;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author F嘉阳
* @date 2018-05-19 16:31
*/
@RestController
@RequestMapping("/get/book")
@Slf4j
public class GetBookController {
@Autowired
private TransportClient client;
/**
* 根据ID查询结果
* @param id
* @return
*/
@GetMapping("/novel")
public ResponseEntity get(@RequestParam(name = "id") String id){
if (id==null){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
GetResponse response = this.client.prepareGet("book","novel",id).get();
if (!response.isExists()){
log.error("【查询为空】ID="+id);
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(response.getSource(), HttpStatus.OK);
}
}