elasticsearch,spring boot,mybatis项目小结,
最近接手一个活,用springboot整合mybatis获取一批数据,上传到elastic search建索引。小小研究了一下,操作不多,没有很难。 很好的教程 http://www.sojson.com/blog/81.html 例 https://yq.aliyun.com/articles/70054遇到比较典型的错: 错1 https://stackoverflow.com/questions/8367312/serializing-with-jackson-json-getting-no-serializer-found No serializer found for class elasticSearch.LogModel and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) 大意是模板类没有set方法,故报错。用对象转json的时候报的。
错2 http://blog.csdn.net/z69183787/article/details/22269131 Jackson2需要引3个maven包 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0.pr3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0.pr3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0.pr3</version>
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9201}]] 这个是因为配置文件中没有配,网上有文章写创建conf,真是乱讲。(后来又报了一次这个错,是因为client连接关闭了还调用连接,spring的@autowire标签初始化类只调用一次构造方法,我创建连接的代码写构造方法里了, 当心client关闭后再次访问,会报找不着节点的错误。)
- $ vi config/elasticsearch.yml
- #cluster name
- cluster.name: sojson-application
- #节点名称
- node.name: node-1
- #绑定IP和端口
- network.host:123.88.88.88
- http.port:9200
不久后又增加功能开放端口,删除整个索引,用标签@RequestMapping("/delete")来实现,在类上和方法上分别加这个标签。参数增加:@RequestParam("indexName") String indexName 再访问,http://127.0.0.1:8099/elastic/delete?indexName=sss 但要注意,标签内的字符串更改后不rebuild可能会认不出来。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 由于访问接口后返回404页面,这一点需要修改。 参考资料http://jinnianshilongnian.iteye.com/blog/1997192 将标签@Service改为@RestController 传参标签由@requestparam改为public String DeleteElastic(@PathVariable("deleteName") String deleteName) {配合 @RequestMapping("/delete/{deleteName}")使用 @PathVariable和@RequestParam,分别是从路径里面去获取变量,也就是把路径当做变量,后者是从请求里面获取参数。 /Springmvc/user/page.do?pageSize=3&pageNow=2 pageSize和pageNow应该是属于参数而不是路径,所以应该添加@RequestParam的注解。 如果做成如下URL,则可以使用@PathVariable /Springmvc/user/page/2/3.do ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 同时还需要注意@Controller和@RestController的区别 @RestController=@Controller+@Reponsebody 只写@Controller会映射到页面,加@Responsebody会直接当作返回结构体来处理,不会对应到页面。 http://blog.csdn.net/gg12365gg/article/details/51345601 ,下面spring程序的代码片段可以直接return回数据,不然需要的是html格式的view。 [java] view plain copy print? @RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; //由于是@RestController,因此直接返回数据,而不是view } } @Service和@Controller区别 @Service用于标注业务层组件 @Controller用于标注控制层组件(如struts中的action)
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。