Skip to content

Commit 33b97d6

Browse files
committed
add example
1 parent a15716f commit 33b97d6

File tree

17 files changed

+1070
-0
lines changed

17 files changed

+1070
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010

1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*
13+
14+
# Eclipse Project
15+
.classpath
16+
.project
17+
.settings/
18+
target/

pom.xml

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.phstudy</groupId>
6+
<artifactId>sample</artifactId>
7+
<name>spring-mvc-elasticsearch</name>
8+
<packaging>war</packaging>
9+
<version>1.0.0-BUILD-SNAPSHOT</version>
10+
<properties>
11+
<java-version>1.8</java-version>
12+
<org.springframework-version>4.0.6.RELEASE</org.springframework-version>
13+
<org.aspectj-version>1.6.10</org.aspectj-version>
14+
<org.slf4j-version>1.6.6</org.slf4j-version>
15+
</properties>
16+
<dependencies>
17+
<!-- Spring -->
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-context</artifactId>
21+
<version>${org.springframework-version}</version>
22+
<exclusions>
23+
<!-- Exclude Commons Logging in favor of SLF4j -->
24+
<exclusion>
25+
<groupId>commons-logging</groupId>
26+
<artifactId>commons-logging</artifactId>
27+
</exclusion>
28+
</exclusions>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-webmvc</artifactId>
33+
<version>${org.springframework-version}</version>
34+
</dependency>
35+
36+
<!-- AspectJ -->
37+
<dependency>
38+
<groupId>org.aspectj</groupId>
39+
<artifactId>aspectjrt</artifactId>
40+
<version>${org.aspectj-version}</version>
41+
</dependency>
42+
43+
<!-- Logging -->
44+
<dependency>
45+
<groupId>org.slf4j</groupId>
46+
<artifactId>slf4j-api</artifactId>
47+
<version>${org.slf4j-version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>jcl-over-slf4j</artifactId>
52+
<version>${org.slf4j-version}</version>
53+
<scope>runtime</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.slf4j</groupId>
57+
<artifactId>slf4j-log4j12</artifactId>
58+
<version>${org.slf4j-version}</version>
59+
<scope>runtime</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>log4j</groupId>
63+
<artifactId>log4j</artifactId>
64+
<version>1.2.15</version>
65+
<exclusions>
66+
<exclusion>
67+
<groupId>javax.mail</groupId>
68+
<artifactId>mail</artifactId>
69+
</exclusion>
70+
<exclusion>
71+
<groupId>javax.jms</groupId>
72+
<artifactId>jms</artifactId>
73+
</exclusion>
74+
<exclusion>
75+
<groupId>com.sun.jdmk</groupId>
76+
<artifactId>jmxtools</artifactId>
77+
</exclusion>
78+
<exclusion>
79+
<groupId>com.sun.jmx</groupId>
80+
<artifactId>jmxri</artifactId>
81+
</exclusion>
82+
</exclusions>
83+
<scope>runtime</scope>
84+
</dependency>
85+
86+
<!-- @Inject -->
87+
<dependency>
88+
<groupId>javax.inject</groupId>
89+
<artifactId>javax.inject</artifactId>
90+
<version>1</version>
91+
</dependency>
92+
93+
<!-- Servlet -->
94+
<dependency>
95+
<groupId>javax.servlet</groupId>
96+
<artifactId>servlet-api</artifactId>
97+
<version>2.5</version>
98+
<scope>provided</scope>
99+
</dependency>
100+
<dependency>
101+
<groupId>javax.servlet.jsp</groupId>
102+
<artifactId>jsp-api</artifactId>
103+
<version>2.1</version>
104+
<scope>provided</scope>
105+
</dependency>
106+
<dependency>
107+
<groupId>javax.servlet</groupId>
108+
<artifactId>jstl</artifactId>
109+
<version>1.2</version>
110+
</dependency>
111+
112+
<!-- Test -->
113+
<dependency>
114+
<groupId>junit</groupId>
115+
<artifactId>junit</artifactId>
116+
<version>4.7</version>
117+
<scope>test</scope>
118+
</dependency>
119+
120+
<!-- ElasticSearch Java API -->
121+
<dependency>
122+
<groupId>org.elasticsearch</groupId>
123+
<artifactId>elasticsearch</artifactId>
124+
<version>1.3.1</version>
125+
</dependency>
126+
127+
<!-- Spring Data ElasticSearch -->
128+
<dependency>
129+
<groupId>org.springframework.data</groupId>
130+
<artifactId>spring-data-elasticsearch</artifactId>
131+
<version>1.0.0.RELEASE</version>
132+
</dependency>
133+
134+
<!-- lorem ipsum generator -->
135+
<dependency>
136+
<groupId>net._01001111</groupId>
137+
<artifactId>jlorem</artifactId>
138+
<version>1.3</version>
139+
</dependency>
140+
</dependencies>
141+
<build>
142+
<plugins>
143+
<plugin>
144+
<artifactId>maven-eclipse-plugin</artifactId>
145+
<version>2.9</version>
146+
<configuration>
147+
<additionalProjectnatures>
148+
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
149+
</additionalProjectnatures>
150+
<additionalBuildcommands>
151+
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
152+
</additionalBuildcommands>
153+
<downloadSources>true</downloadSources>
154+
<downloadJavadocs>true</downloadJavadocs>
155+
</configuration>
156+
</plugin>
157+
<plugin>
158+
<groupId>org.apache.maven.plugins</groupId>
159+
<artifactId>maven-compiler-plugin</artifactId>
160+
<version>2.5.1</version>
161+
<configuration>
162+
<source>1.8</source>
163+
<target>1.8</target>
164+
<compilerArgument>-Xlint:all</compilerArgument>
165+
<showWarnings>true</showWarnings>
166+
<showDeprecation>true</showDeprecation>
167+
</configuration>
168+
</plugin>
169+
<plugin>
170+
<groupId>org.codehaus.mojo</groupId>
171+
<artifactId>exec-maven-plugin</artifactId>
172+
<version>1.2.1</version>
173+
<configuration>
174+
<mainClass>org.test.int1.Main</mainClass>
175+
</configuration>
176+
</plugin>
177+
</plugins>
178+
</build>
179+
</project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.phstudy.sample.controller;
2+
3+
import java.text.DateFormat;
4+
import java.util.ArrayList;
5+
import java.util.Date;
6+
import java.util.List;
7+
import java.util.Locale;
8+
import java.util.UUID;
9+
10+
import javax.annotation.PostConstruct;
11+
import javax.annotation.Resource;
12+
13+
import net._01001111.text.LoremIpsum;
14+
15+
import org.phstudy.sample.model.Book;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
19+
import org.springframework.data.elasticsearch.core.query.IndexQuery;
20+
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
21+
import org.springframework.stereotype.Controller;
22+
import org.springframework.ui.Model;
23+
import org.springframework.web.bind.annotation.RequestMapping;
24+
import org.springframework.web.bind.annotation.RequestMethod;
25+
26+
/**
27+
* Handles requests for the application home page.
28+
*/
29+
@Controller
30+
public class HomeController {
31+
32+
private static final Logger logger = LoggerFactory
33+
.getLogger(HomeController.class);
34+
35+
@Resource
36+
private ElasticsearchTemplate elasticsearchTemplate;
37+
38+
private static final int MINCOUNT = 100;
39+
private static final int MAXCOUNT = 200;
40+
41+
@PostConstruct
42+
private void initData() {
43+
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
44+
LoremIpsum lorem = new LoremIpsum();
45+
for (int i = MINCOUNT; i < MAXCOUNT; i++) {
46+
String documentId = UUID.randomUUID().toString();
47+
Book book = new Book();
48+
book.setId(documentId);
49+
book.setMessage(lorem.paragraph());
50+
IndexQuery indexQuery = new IndexQueryBuilder()
51+
.withId(book.getId()).withObject(book).build();
52+
indexQueries.add(indexQuery);
53+
}
54+
55+
// bulk index
56+
elasticsearchTemplate.bulkIndex(indexQueries);
57+
}
58+
59+
/**
60+
* Simply selects the home view to render by returning its name.
61+
*/
62+
@RequestMapping(value = "/", method = RequestMethod.GET)
63+
public String home(Locale locale, Model model) {
64+
logger.info("Welcome home! The client locale is {}.", locale);
65+
66+
Date date = new Date();
67+
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
68+
DateFormat.LONG, locale);
69+
70+
String formattedDate = dateFormat.format(date);
71+
72+
model.addAttribute("serverTime", formattedDate);
73+
74+
return "home";
75+
}
76+
77+
@RequestMapping(value = "/httpservletrequest", method = RequestMethod.GET)
78+
public String httpservletrequest(Locale locale, Model model) {
79+
return "httpservletrequest";
80+
}
81+
82+
@RequestMapping(value = "/pageable", method = RequestMethod.GET)
83+
public String pageable(Locale locale, Model model) {
84+
return "pageable";
85+
}
86+
87+
@RequestMapping(value = "/requestparam", method = RequestMethod.GET)
88+
public String requestparam(Locale locale, Model model) {
89+
return "requestparam";
90+
}
91+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.phstudy.sample.controller.rest;
2+
3+
import javax.annotation.Resource;
4+
import javax.servlet.http.HttpServletRequest;
5+
6+
import org.apache.commons.lang.StringUtils;
7+
import org.phstudy.sample.model.Book;
8+
import org.phstudy.sample.repository.BookRepository;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.data.domain.Page;
12+
import org.springframework.data.domain.PageRequest;
13+
import org.springframework.data.domain.Pageable;
14+
import org.springframework.data.web.PageableDefault;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RequestMethod;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
@RestController
21+
public class BookRestController {
22+
@Resource
23+
private BookRepository bookRepository;
24+
25+
private static final Logger logger = LoggerFactory
26+
.getLogger(BookRestController.class);
27+
28+
@RequestMapping(value = "/rest/book1", method = RequestMethod.GET)
29+
public Page<Book> usePageable(
30+
@RequestParam(value = "query", required = false) String query,
31+
@PageableDefault(size = 10) Pageable pageable) {
32+
33+
logger.info("/rest/book1 -> pageSize = {}, pageNumber = {}",
34+
pageable.getPageNumber(), pageable.getPageSize());
35+
36+
if (StringUtils.isBlank(query)) {
37+
return bookRepository.findAll(pageable);
38+
}
39+
return bookRepository.findByMessage(query, pageable);
40+
}
41+
42+
@RequestMapping(value = "/rest/book2", method = RequestMethod.GET)
43+
public Page<Book> useHttpServletRequest(
44+
@RequestParam(value = "query", required = false) String query,
45+
HttpServletRequest request) {
46+
47+
int size = 10;
48+
int page = Integer.parseInt(request.getParameter("page"));
49+
50+
logger.info("/rest/book2 -> pageNumber = {}", page);
51+
52+
Pageable pageable = new PageRequest(page, size);
53+
54+
if (StringUtils.isBlank(query)) {
55+
return bookRepository.findAll(pageable);
56+
}
57+
return bookRepository.findByMessage(query, pageable);
58+
}
59+
60+
@RequestMapping(value = "/rest/book3", method = RequestMethod.GET)
61+
public Page<Book> useRequestParam(
62+
@RequestParam(value = "query", required = false) String query,
63+
@RequestParam(value = "page") int page) {
64+
65+
int size = 10;
66+
67+
logger.info("/rest/book3 -> pageNumber = {}", page);
68+
69+
Pageable pageable = new PageRequest(page, size);
70+
71+
if (StringUtils.isBlank(query)) {
72+
return bookRepository.findAll(pageable);
73+
}
74+
return bookRepository.findByMessage(query, pageable);
75+
}
76+
}

0 commit comments

Comments
 (0)