Tag Archives: Tutorial

Allow Remote Access On MongoDB

By default MongoDB doesn’t allow remote connections. MongoDB bind to local interface only, it will restrict the remote connections, besides that to allow LAN connections from Application Server open and modify your mongod.conf file like below. In this tutorial i’m using: Debian 9 Linux 64bit x64 MongoDB v4.0.2 Community Server Compute Engine on Google cloud platform Allow Remote Access On MongoDB… Read More »

Spring Data MongoDB – Upsert

In this tutorial i will share to you how to use UpSert operation in spring data mongoDB. If no document that matches the query, a new document is created by combining the query and update object. @Autowired private MongoTemplate mongoTemplate; //search a document that doesn’t exist Query query = new Query(); query.addCriteria(Criteria.where(“id”).is(“cimblo”)); Update update = new Update(); update.set(“age”,… Read More »

Spring Data MongoDB – Save Or Update

In this tutorial you will learn how to save or update data into database using spring data mongoDB. Spring Data MongoDB – Save Or Update First declare “Mongo Template”, then do save or update : @Autowired private MongoTemplate mongoTemplate; Mongo Template Save User user = new User(); user.setName(“cimblo”); user.setAge(99); //save data with save() function mongoTemplate.save(userTest1); Mongo Template Update… Read More »

Spring Data MongoDB LIKE Query Insensitive Case ‘%%’

With spring data mongoDB you can do “LIKE QUERY” like example below. Query query = new Query(); query.addCriteria(Criteria.where(“name”).regex(“yourString”, “i”)); mongoTemplate.find(query, CodingExample.class) Spring Data MongoDB LIKE Query Insensitive Case Spring Data MongoDB The Spring Data MongoDB project provides integration with the MongoDB document database. Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with… Read More »

Convert Array to List In Java 8

In java 8 you can easily to convert array to list with stream. Convert Array to List In Java 8 For example: String[] array = new String[10]; List<String> listCollection = Arrays.stream(array).collect(Collectors.toList()); ArrayList<String> arrayListCollection = Arrays.stream(array).collect(Collectors.toCollection(ArrayList::new)); Set<String> setCollection = Arrays.stream(array).collect(Collectors.toSet()); HashSet<String> hashSetCollection = Arrays.stream(array).collect(Collectors.toCollection(HashSet::new)); TreeSet<String> treeSetCollection = Arrays.stream(array).collect(Collectors.toCollection(TreeSet::new)); LinkedList<String> linkedListCollection = Arrays.stream(array).collect(Collectors.toCollection(LinkedList::new)); LinkedHashSet<String> linkedHashSetCollection = Arrays.stream(array).collect(Collectors.toCollection(LinkedHashSet::new)); ArrayDeque<String> arrayDequeCollection =… Read More »

REST API Return Image With Spring

How to create rest controller for return image file? Here is the code for create image rest API with spring: @GetMapping( value = “/img”, produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE}) public ResponseEntity getImageAsResource() { HttpHeaders headers = new HttpHeaders(); String home = System.getProperty(“user.home”); File f = new File(home + File.separator + “Desktop” + File.separator + “image.jpg”); Resource resource =… Read More »

Download Image From URL In Java

Simple way to download image from url in java is like this: String source = “https://www.google.co.id/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png”; URL url = new URL(source); URLConnection urlConnection = url.openConnection(); InputStream in = urlConnection.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1!=(n=in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); String home… Read More »