我们应该注意到用户的个人兴趣,兴趣可以是多个,这里所说有兴趣就是用户要search的对象。这里查找的对象是一个列表的形式。这里有一个有趣的事,在用户访问的URL地址的请求参数是:someUrl/param?var1=value1&var2=value2。列表的请求参数是:someUrl/param;var1=value1,value2;var2=value3,value4。在我们的例子中我们想要处理的是这样的访问URL地址:http://localhost:8080/search/popular;keywords=scala,java。
默认的情况下,SpringMVC会删除掉带有分号的URL地址请求。第一件事,就是要让我们请求的多值URL地址是有效的。让我们在WebConfiguration中添加下面的代码:
@Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); }
现在我们需要在创建个search的包,在ctroller的包下。创建一个类的名为SearchController.
package masterSpringMvc.search; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.social.twitter.api.Tweet; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.MatrixVariable; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller public class SearchController { private SearchService searchService; @Autowired public SearchController(SearchService searchService) { this.searchService = searchService; } @RequestMapping("/search/{searchType}") public ModelAndView search(@PathVariable String searchType, @ MatrixVariable List<String> keywords) { List<Tweet> tweets = searchService.search(searchType, keywords); ModelAndView modelAndView = new ModelAndView("resultPage"); modelAndView.addObject("tweets", tweets); modelAndView.addObject("search", String.join(",", keywords)); return modelAndView; } }
在上面的类中,我们注入了SearchService所以我们还要创建这个类。
package masterSpringMvc.search; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.social.twitter.api.Tweet; import org.springframework.social.twitter.api.Twitter; import org.springframework.stereotype.Service; import java.util.List; @Service public class SearchService { private Twitter twitter; @Autowired public SearchService(Twitter twitter) { this.twitter = twitter; } public List<Tweet> search(String searchType, List<String> keywords) { return null; } }
由于search是通过twitter.searchOperations().search(params)来获取的,searchParameters作为有效的查找依据。首先,我们需要在searchType的路径下创建ResultType的控制器,使searchaType的路径是有效的。ResultType是一个枚举,所以可以遍历出不同的值,找到我们需要查找的值。
private SearchParameters.ResultType getResultType(String searchType) { for (SearchParameters.ResultType knownType : SearchParameters. ResultType.values()) { if (knownType.name().equalsIgnoreCase(searchType)) { return knownType; } } return SearchParameters.ResultType.RECENT; }
我们现在创建SearchParameters的控制器,方法如下:
private SearchParameters createSearchParam(String searchType, String taste) { SearchParameters.ResultType resultType = getResultType(searchType); SearchParameters searchParameters = new SearchParameters(taste); searchParameters.resultType(resultType); searchParameters.count(3); return searchParameters; }
最后,我们来看一下笔者写好的SearchService的完整类。
package masterSpringMVC.search; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.social.twitter.api.SearchParameters; import org.springframework.social.twitter.api.Tweet; import org.springframework.social.twitter.api.Twitter; import org.springframework.stereotype.Service; import java.util.List; import java.util.stream.Collectors; /** * Created by OwenWilliam on 2016/5/19. */ @Service public class SearchService { private Twitter twitter; @Autowired public SearchService(Twitter twitter) { this.twitter = twitter; } /** * 查找 * @param searchType * @param keywords * @return */ public List<Tweet> search(String searchType, List<String> keywords) { //(taking a list of keywords and returning a SearchParameters constructor for each one List<SearchParameters> searches = keywords.stream().map(taste -> createSearchParam(searchType,taste)) .collect(Collectors.toList()); //to fetch the tweets for each SearchParameters constructor List<Tweet> result = searches.stream().map(params -> twitter.searchOperations().search(params)) .flatMap(searchResults -> searchResults.getTweets().stream()) .collect(Collectors.toList()); return result; } /** * 去Twitter上查找分类的内容,如:mixed、recent、popular * @param searchType * @return */ private SearchParameters.ResultType getResultType(String searchType) { for (SearchParameters.ResultType knowType : SearchParameters.ResultType.values()) { //返回是要查找的对象 if (knowType.name().equalsIgnoreCase(searchType)) { return knowType; } } return SearchParameters.ResultType.RECENT; } /** * 配搭符合的分类,和对应分类 的内容 * @param searchType 分类 * @param taste 具体,如java/scala等 * @return */ private SearchParameters createSearchParam(String searchType, String taste) { //分类的 SearchParameters.ResultType resultType = getResultType(searchType); //对应的具体的 SearchParameters searchParameters = new SearchParameters(taste); searchParameters.resultType(resultType); searchParameters.count(3); return searchParameters; } }
我们启动项目,在地址栏上输入:http://localhost:8080/search/
mixed;keywords=scala,java,你将会看到有结果如下: