It is very easy to create a rest service. We even do not need a servlet container to run the application. Spring Boot will prepare a container for us. We only need to add what is necessary. Even a web.xml file is not necessary.
This rest service searches a text in elasticsearch and returns the result as JSON. Amount of code needed for this task is amazingly low.
First class is the controller. Request will be served with this controller. For elasticsearch repository see [1].
Now comes the spring boot application entry point.
In order to use the rest service use the request like this:
http://localhost:8080/recipe/by-name?name=mynameis
For complete code listing in github see [2].
1. Spring Boot Elasticsearch Application
2. Github - essync
This rest service searches a text in elasticsearch and returns the result as JSON. Amount of code needed for this task is amazingly low.
First class is the controller. Request will be served with this controller. For elasticsearch repository see [1].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
public class RecipeRestController { | |
@Autowired | |
private RecipeRepositoryES recipeRepositoryES; | |
@RequestMapping("/recipe/by-name") | |
public List<RecipeEntityES> greeting(@RequestParam(value = "name", defaultValue = "pasta") String name) { | |
return recipeRepositoryES.findByName(name); | |
} | |
} |
Now comes the spring boot application entry point.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootApplication | |
@EnableElasticsearchRepositories(basePackages = "cookle.core.esdao") | |
public class RestMain { | |
public static void main(String[] args) { | |
SpringApplication.run(RestMain.class, args); | |
} | |
} |
In order to use the rest service use the request like this:
http://localhost:8080/recipe/by-name?name=mynameis
For complete code listing in github see [2].
1. Spring Boot Elasticsearch Application
2. Github - essync
Comments
Post a Comment