diff --git a/src/main/java/top/fjy8018/elasticsearch/Controller/GetBookController.java b/src/main/java/top/fjy8018/elasticsearch/Controller/GetBookController.java new file mode 100644 index 0000000..9e7e563 --- /dev/null +++ b/src/main/java/top/fjy8018/elasticsearch/Controller/GetBookController.java @@ -0,0 +1,45 @@ +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") +@Slf4j +public class GetBookController { + + @Autowired + private TransportClient client; + + /** + * 根据ID查询结果 + * @param id + * @return + */ + @GetMapping("/book/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); + } +} diff --git a/src/main/java/top/fjy8018/elasticsearch/ElasticsearchApplication.java b/src/main/java/top/fjy8018/elasticsearch/ElasticsearchApplication.java index 70e97e6..a7e533f 100644 --- a/src/main/java/top/fjy8018/elasticsearch/ElasticsearchApplication.java +++ b/src/main/java/top/fjy8018/elasticsearch/ElasticsearchApplication.java @@ -1,52 +1,17 @@ package top.fjy8018.elasticsearch; - -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.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; @SpringBootApplication -@RestController -@Slf4j public class ElasticsearchApplication { - @Autowired - private TransportClient client; - @GetMapping("/") public String index(){ return "index"; } - /** - * 根据ID查询结果 - * @param id - * @return - */ - @GetMapping("/get/book/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); - } - public static void main(String[] args) { SpringApplication.run(ElasticsearchApplication.class, args); }