轉(zhuǎn)眼間第二季來到了第五章止潮,也是我們博客系統(tǒng)的第四章窃判。前段時間因為個人私事較多,項目停更了兩期沽翔,但是這都不是問題兢孝,我們繼續(xù)接著走下去。畢竟承諾的事情就得完成仅偎。
這一期我們的目標(biāo)是完成后端博客系統(tǒng)的登錄功能和添加Apis-doc功能跨蟹。
博文發(fā)布系統(tǒng)
我們需要發(fā)布博文,那么后端必不可少的是登錄和發(fā)布系統(tǒng)橘沥,至于其他的我們可以先緩一緩窗轩,畢竟我也沒想好后端頁面怎么設(shè)計,嘿嘿座咆。
前面我看了一下痢艺,確實是完美兼容WordPress還是有很多難度,畢竟很多技術(shù)細(xì)節(jié)我們并不知道介陶,不過堤舒,至少說目前我們已經(jīng)兼容了博客文章,剩下的只需要一點點的適配就能大概完成任務(wù)哺呜。
不多說了舌缤,我們先完成后端登錄功能。
后端登錄
后端登錄,我們不可能說一味的兼容WordPress国撵,還有一些技術(shù)上面的設(shè)計理念可能也不是那么那啥陵吸,所以我們需要拿出一些自己的玩意。首先還是老規(guī)矩介牙,從Dao→Service→Controller壮虫。
- Dao按照老規(guī)矩就是對數(shù)據(jù)庫的操作,所以我們只需要寫上接口和mapper就行了环础。
- Service層還是一樣進行單元數(shù)據(jù)操作囚似。
- Controller是web應(yīng)用的入口地點。
有了上面的這些我們只需要進行一個登錄驗證喳整,也就是前面說過的密碼規(guī)則驗證谆构,不過具體代碼如下:
@RequestMapping(value = "/login"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = RequestMethod.POST)
@ApiOperation(
value = "用戶登錄"
, notes = "用戶登錄的接口,輸入用戶名和密碼進行登錄"
, response = User.class)
@ResponseBody
public Object userLogin(HttpServletRequest request,
@ApiParam(value = "用戶名不能為空框都,否則不允許登錄"
, required = true) @RequestParam("userLogin") String userLogin,
@ApiParam(value = "用戶密碼不能為空且必須為16位小寫MD5搬素,否則不允許登錄"
, required = true) @RequestParam("userPass") String userPass) {
ResponseObj<User> responseObj = new ResponseObj<>();
User user;
if (PublicUtil.isJsonRequest(request)) { //確認(rèn)是否是post的json提交
try {
user = new GsonUtils().jsonRequest2Bean(request.getInputStream(), User.class); //轉(zhuǎn)換為指定類型的對象
userLogin = user.getUserLogin();
userPass = user.getUserPass();
} catch (IOException e) {
e.printStackTrace();
}
}
if (StringUtils.isEmpty(userLogin) || StringUtils.isEmpty(userPass)) {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("用戶名和密碼不能為空!請檢查魏保!");
return new GsonUtils().toJson(responseObj);
}
user = userService.findOneById(userLogin);
if (null == user) {
responseObj.setCode(ResponseObj.EMPUTY);
responseObj.setMsg("用戶不存在熬尺!請檢查!");
return new GsonUtils().toJson(responseObj);
}
userPass = userPass.toLowerCase(); //將大寫md5轉(zhuǎn)換為小寫md5
if (userPass.length() > 16 && userPass.length() == 32) { //32位小寫轉(zhuǎn)換為16位小寫
userPass = userPass.substring(8, 24).toLowerCase();
} else if (userPass.length() > 32) {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("密碼不規(guī)范!請檢查谓罗!");
return new GsonUtils().toJson(responseObj);
}
String encryptPassword = EncryptUtils.encryptPassword(userPass, user.getUserActivationKey());
if (!encryptPassword.equals(user.getUserPass())) {
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg("用戶名和密碼不匹配粱哼!請檢查!");
return new GsonUtils().toJson(responseObj);
}
user.setUserPass(request.getSession().getId()); //將sessionId放入用戶信息中
user.setUserActivationKey(""); //將用戶注冊的salt清空
user.setUserUrl("/user/endSupport/index");
responseObj.setData(user);
responseObj.setCode(ResponseObj.OK);
responseObj.setMsg("登錄成功");
return new GsonUtils().toJson(responseObj);
}
雖然說很多東西我們在前端或者是客戶端已經(jīng)做了限制檩咱,但是為了防止別人搞事情我們還是需要這樣做才行揭措。
Spring-Fox,Api測試頁面
什么是Spring-Fox呢刻蚯?Springfox的前身是swagger-springmvc绊含,是一個開源的API doc框架,可以將我們的Controller的方法以文檔的形式展現(xiàn)炊汹。
為什么我們要大費周章的做這些呢躬充?
- 它可以幫助我們歸類web訪問入口
- 它可以整理接口
- 它可以···
確實語言描述是我的弱點,不過呢讨便,我這個理工直男癌就需要直截了當(dāng)?shù)恼f出來充甚,沒時間解釋,直接上圖霸褒。

正如上面的截圖所示伴找,我們首先應(yīng)該找到對應(yīng)的spring-fox的說明文檔,然后仔細(xì)一看網(wǎng)上分為兩個版本废菱,一個是開源中國的引入說明技矮,一個是Spring-Fox官方的使用說明眉反,那么肯定選擇官方的。
按照官方文檔穆役,我們簡單總結(jié)一下:
- 版本選擇(Release或者Snapshot,推薦使用Release)
- 依賴引入(maven或者gradle)
- swagger設(shè)置
- 重要細(xì)節(jié)(Spring-Fox官方文檔中沒有指明J崃荨9⒈摇!)
按照官方文檔說明的是韧拒,他們的demo是在SpringBoot下面實現(xiàn)的淹接,現(xiàn)在我們需要單一的拆分出來,可以看成我們的項目就是Spring-Mvc叛溢,所以一些細(xì)節(jié)需要改變塑悼,當(dāng)然當(dāng)中一個很重要的細(xì)節(jié)官方文檔也是沒有指明,所以看官們且看我細(xì)細(xì)道來楷掉。
引入依賴資源
首先我們引入引來資源厢蒜,通讀全文最基本的依賴是:springfox-swagger、springfox-swagger-ui烹植,所以我們直接老規(guī)矩斑鸦,在gradle的配置文件中引入依賴:
compile "io.springfox:springfox-swagger2:2.6.1"
compile 'io.springfox:springfox-swagger-ui:2.6.1'
在引入上面的基本依賴后,我們查看他們關(guān)聯(lián)的依賴可以發(fā)現(xiàn)這些依賴?yán)锩孢€有引入jackson草雕,這個時候我們可以選擇提升我們的Jackson或者不管他們也行巷屿,不過我還是吧Jackson的版本提升了:
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.4'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.4'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.4'
swagger設(shè)置
根據(jù)官方文檔我們可以看到有一個swagger設(shè)置需要先引入后,才能讓我們設(shè)定的東西生效墩虹,所以我們先引入設(shè)置:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Created by mac on 2017/1/8.
*/
@Configuration //說明這個是spring的設(shè)置
@EnableWebMvc //不是SpringBoot需要引入這個
@EnableSwagger2 //開啟Swagger2
@ComponentScan("cn.acheng1314.controller") //指定被掃描Controller的位置
public class Swagger2SpringMvc extends WebMvcConfigurerAdapter {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) //Docket嘱巾,Springfox的私有API設(shè)置初始化為Swagger2
.select()
// .apis(RequestHandlerSelectors.basePackage("cn.acheng1314.controller")) //此處設(shè)置掃描包和上面設(shè)置的掃描包一樣效果
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfoBuilder() //設(shè)置API文檔的主體說明
.title("博客Apis")
.description("雨下一整夜的博客APIs")
.version("1.01")
.termsOfServiceUrl("http://acheng1314.cn/")
.build())
// .pathMapping("/")
// .genericModelSubstitutes(ResponseEntity.class)
// .alternateTypeRules(
// newRule(typeResolver.resolve(DeferredResult.class,
// typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
// typeResolver.resolve(WildcardType.class)))
;
}
}
設(shè)置完成上面的東西后,我們需要干什么呢诫钓? 上面我們很明顯的看到我們 @configuration是一個spring框架的注解 旬昭,顧名思義肯定就應(yīng)該是一個spring的設(shè)置。同時我們可以在idea的編輯器中看到類名有一層淡黃色的標(biāo)記尖坤,然后選中類名按下代碼提示(Alt+Enter)會有提示告訴我們這個設(shè)置沒有使用稳懒,然后自動完成后會給我們自動添加到Spring的ApplicationContext中作為CodeContext使用。
當(dāng)然慢味,上面的是懶人做事這樣做的后果會是導(dǎo)致apiInfo的設(shè)置不能生效场梆。
那么正常一點的應(yīng)該是怎么做呢?按照Spring的思想來說纯路,我們需要在Spring的設(shè)置文件中直接引入bean或油。所以我們在spring-web.xml中插入對應(yīng)的bean,如下:
<!--SpringFox設(shè)置引入-->
<bean id="SpringFox" class="cn.acheng1314.Swagger2SpringMvc"/>
通過這樣的在spring的配置文件中設(shè)置后驰唬,我們感覺應(yīng)該是能用的顶岸,所以我們可以先跑起來看看腔彰。
重要細(xì)節(jié),我有特殊的裝X技巧
按照官方文檔我們完全設(shè)定了后辖佣,我們可以看到就算我們在代碼中引入了配置后霹抛,一樣的在里面不能看到網(wǎng)頁的接口列表(只看得到上面的標(biāo)題欄,下面的是空白)卷谈,然后我們仔細(xì)的查看網(wǎng)頁的請求會發(fā)現(xiàn)
http://localhost:8080/swagger-resources/configuration/ui
這個請求是404杯拐。說實話這個錯誤困擾了我很久,同時這個問題前面我處理的時候還是有一系列的綜合問題世蔗,后面整個工程師重建后完成的端逼。
但是現(xiàn)在這個問題簡單直接找到問題所在了,那就是在我們spring的設(shè)置中污淋,關(guān)于web的設(shè)置我們都是在spring-web.xml中完成的顶滩,同時里面的東西我們需要改動一下才能適應(yīng)現(xiàn)在的需要,如下:
<!-- 4.掃描web相關(guān)的bean -->
<context:component-scan base-package="cn.acheng1314.controller">
<!-- 制定掃包規(guī)則 ,只掃描使用@Controller注解的JAVA類 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--上面的是原來的設(shè)置寸爆,現(xiàn)在我們新的代碼如下:-->
<!-- 4.掃描web相關(guān)的bean -->
<context:component-scan base-package="cn.acheng1314.controller,springfox">
<!-- 制定掃包規(guī)則 ,只掃描使用@Controller注解的JAVA類 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
也就是說礁鲁,我們除了要在Spring的配置文件中引入bean來初始化swagger相關(guān)的東西以外,我們還需要在web掃描那里添加springfox的掃描赁豆。所以我們spring-fox的設(shè)置相關(guān)的完成了救氯。
Spring-Fox的使用
從前面的學(xué)習(xí)中我們可以明白我們所有的網(wǎng)絡(luò)請求都是在controller中來實現(xiàn)的,所以我們這里需要通過對controller做適當(dāng)?shù)男薷牟拍軐崿F(xiàn)SpringFox的使用歌憨。具體的直接仍代碼上來着憨,大家詳細(xì)的看看就行了,不需要什么深入鉆研务嫡。
import cn.acheng1314.domain.*;
import cn.acheng1314.domain.Response.HomeBean;
import cn.acheng1314.service.postService.PostService;
import cn.acheng1314.service.userService.UserService;
import cn.acheng1314.utils.GsonUtils;
import cn.acheng1314.utils.PublicUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
/**
* 前端頁面的控制器甲抖,博客系統(tǒng)前端頁面相對較少,所以都扔在這里了
* Created by 程 on 2016/11/25.
*/
@Controller
@RequestMapping("/front")
@Api(value = "/front", description = "前臺頁面")
public class FrontWebController {
@Autowired
private PostService postService;
@Autowired
private UserService userService;
/**
* 返回主頁面
*
* @return
*/
@RequestMapping(value = "/main", method = RequestMethod.GET)
@ApiOperation(
value = "打開首頁界面"
, notes = "首頁web界面心铃,js模板加載網(wǎng)頁數(shù)據(jù)")
public ModelAndView frontMain(HttpServletRequest request) throws Exception {
ModelAndView view = new ModelAndView("frontMain");
view.addObject("framJson", getFramJson());
view.addObject("postsJson", findPublishPost(request, 1, 10));
return view;
}
/**
* 獲取文章分頁列表
*
* @param request 用戶請求
* @param pageNum 當(dāng)前頁碼
* @param pageSize 每一頁的長度
* @return
* @throws Exception
*/
@RequestMapping(value = "/findPublishPost"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
@ApiOperation( //接口描述
value = "獲取文章分頁列表" //功能簡介
, notes = "返回文章列表准谚,分頁加載" //接口功能說明
, response = PostBean.class, //返回數(shù)據(jù)的值說明
responseContainer = "List") //返回數(shù)據(jù)類型說明
public Object findPublishPost(HttpServletRequest request,
@ApiParam(value = "當(dāng)前頁碼,默認(rèn)不能為空,否則為1",
required = true, //參數(shù)是否必傳
defaultValue = "1" //參數(shù)默認(rèn)值為1
// ,allowableValues = "available,pending,sold" //暫時未知去扣,待查閱文章
// ,allowMultiple = true //是否允許allowMultiple類型的參數(shù)
) @RequestParam("pageNum")
int pageNum,
@ApiParam(value = "每一頁的長度,默認(rèn)不能為空柱衔,否則列表條目數(shù)量為10",
required = true, //參數(shù)是否必傳
defaultValue = "10" //參數(shù)默認(rèn)值為1
// ,allowableValues = "available,pending,sold" //暫時未知,待查閱文章
// ,allowMultiple = true //是否允許allowMultiple類型的參數(shù)
) @RequestParam("pageSize")
int pageSize) throws Exception {
PageSplit page;
ResponseList<PostBean> list = new ResponseList<>();
if (PublicUtil.isJsonRequest(request)) { //確認(rèn)是否是post的json提交
page = new GsonUtils().jsonRequest2Bean(request.getInputStream(), PageSplit.class); //轉(zhuǎn)換為指定類型的對象
pageNum = page.getPageNum();
pageSize = page.getPageSize();
}
if (pageNum <= 0) {
pageNum = 1;
}
if (pageSize == 0) {
pageSize = 10;
}
try {
int toalNum; //總頁碼
toalNum = postService.getAllCount(); //先把總條數(shù)賦值給總頁數(shù)愉棱,作為緩存變量算墨,減少下面算法的查找次數(shù)
toalNum = toalNum % pageSize > 0 ? toalNum / pageSize + 1 : toalNum / pageSize; //在每頁固定條數(shù)下能不能分頁完成沐旨,有余則加一頁碼
List<PostBean> tmp = postService.findAllPublish(pageNum, pageSize);
if (null == tmp || tmp.size() == 0) {
list.setCode(ResponseList.EMPUTY);
list.setMsg(ResponseList.EMPUTY_STR);
return new GsonUtils().toJson(list);
}
list.setCode(ResponseList.OK);
list.setMsg(ResponseList.OK_STR);
list.setPageNum(pageNum);
list.setTotalNum(toalNum);
list.setPageSize(pageSize);
list.setData(tmp);
return new GsonUtils().toJson(list);
} catch (Exception e) {
e.printStackTrace();
//查詢失敗
list.setCode(ResponseObj.FAILED);
list.setMsg(ResponseList.FAILED_STR);
return new GsonUtils().toJson(list);
}
}
/**
* 查找最近的文章
*
* @return
*/
@RequestMapping(value = "/findNewPost"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(
value = "獲取最近文章"
, notes = "獲取最近文章的json存淫,具體字段請參照輸出的json數(shù)據(jù)"
, response = PostBean.class)
@ResponseBody
public Object findNewPost() {
ResponseObj<Object> responseObj = new ResponseObj<>();
try {
List<PostBean> allNew = postService.findAllNew();
if (null == allNew || allNew.isEmpty()) {
responseObj.setCode(ResponseObj.EMPUTY);
responseObj.setMsg(ResponseObj.EMPUTY_STR);
} else {
responseObj.setCode(ResponseObj.OK);
responseObj.setMsg(ResponseObj.OK_STR);
responseObj.setData(allNew);
}
return new GsonUtils().toJson(responseObj);
} catch (Exception e) {
e.printStackTrace();
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg(ResponseObj.FAILED_STR);
return new GsonUtils().toJson(responseObj);
}
}
/**
* 獲取熱點文章信息
*
* @return
*/
@RequestMapping(value = "/findHotPost"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(
value = "獲取最熱文章"
, notes = "獲取最熱文章的json谜喊,具體字段請參照輸出的json數(shù)據(jù)"
, response = PostBean.class)
@ResponseBody
public Object findHotPost() {
return findNewPost();
}
/**
* 獲取隨機文章信息
*
* @return 返回json
*/
@RequestMapping(value = "/findRandomPost"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(
value = "隨機獲取文章"
, notes = "隨機獲取文章的json,具體字段請參照輸出的json數(shù)據(jù)"
, response = PostBean.class)
@ResponseBody
public Object findRandomPost() {
return findNewPost();
}
/**
* 獲取主頁的json數(shù)據(jù)朋其,按照道理講這里應(yīng)該根據(jù)頁面結(jié)構(gòu)拆分組合的
*
* @param user 用戶信息
* @return 返回首頁json
*/
@RequestMapping(value = "/home"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
@Deprecated
public Object getHomeJson(User user) {
if (null != user) {
//埋點王浴,AOP日志記錄
}
HomeBean homeBean = new HomeBean(); //首頁內(nèi)容
HomeBean.DataBean dataBean = new HomeBean.DataBean(); //首頁下面的Data內(nèi)容對象
try {
int toalNum; //總頁碼
toalNum = postService.getAllCount(); //先把總條數(shù)賦值給總頁數(shù)脆炎,作為緩存變量,減少下面算法的查找次數(shù)
toalNum = toalNum % 10 > 0 ? toalNum / 10 + 1 : toalNum / 10; //在每頁固定條數(shù)下能不能分頁完成氓辣,有余則加一頁碼
List<PostBean> postsData = postService.findAllPublish(1, 10); //首頁下面的文章內(nèi)容
List<PostBean> newData = postService.findAllNew(); //首頁下面的文章內(nèi)容
if (null == postsData || postsData.isEmpty()) {
dataBean.setPosts(null);
} else {
dataBean.setPosts(postsData); //首頁文章列表信息設(shè)定
}
if (null == newData || newData.isEmpty()) {
dataBean.setNewPosts(null);
dataBean.setHotPosts(null);
dataBean.setRandomPosts(null);
} else {
dataBean.setNewPosts(newData); //首頁文章列表信息設(shè)定
dataBean.setHotPosts(newData);
dataBean.setRandomPosts(newData);
}
List<DateCountBean> allPostDateCount = postService.getAllPostDateCount();
if (null != allPostDateCount && !allPostDateCount.isEmpty()) {
dataBean.setDate(allPostDateCount);
} else {
dataBean.setDate(null);
}
//設(shè)置作者信息
List<HashMap<String, String>> userMeta = userService.getUserMeta(1);
dataBean.setAuthor(userMeta);
dataBean.setPageNum(1);
dataBean.setPageSize(10);
dataBean.setTotalNum(toalNum);
homeBean.setData(dataBean);
homeBean.setCode(ResponseObj.OK);
homeBean.setMsg(ResponseList.OK_STR);
return new GsonUtils().toJson(homeBean);
} catch (Exception e) {
e.printStackTrace();
//查詢失敗
homeBean.setCode(ResponseObj.FAILED);
homeBean.setMsg(ResponseList.FAILED_STR);
return new GsonUtils().toJson(homeBean);
}
}
/**
* 頁面框架的變化信息
* 1秒裕、個人信息
* 2、最新熱點隨機文章信息
* 3钞啸、標(biāo)簽信息
*
* @return
*/
@RequestMapping(value = "/getFramJson"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(
value = "獲取主題框架的json"
, notes = "整個頁面的主體框架的json數(shù)據(jù)")
@ResponseBody
public Object getFramJson() {
HomeBean homeBean = new HomeBean(); //首頁內(nèi)容
HomeBean.DataBean dataBean = new HomeBean.DataBean(); //首頁下面的Data內(nèi)容對象
try {
List<PostBean> newData = postService.findAllNew();
if (null == newData || newData.isEmpty()) {
//頁面上面推薦的文章信息不為空
dataBean.setNewPosts(null);
dataBean.setHotPosts(null);
dataBean.setRandomPosts(null);
} else {
//首頁文章列表信息設(shè)定
dataBean.setNewPosts(newData);
dataBean.setHotPosts(newData);
dataBean.setRandomPosts(newData);
}
//日期歸檔
List<DateCountBean> allPostDateCount = postService.getAllPostDateCount();
if (null != allPostDateCount && !allPostDateCount.isEmpty()) {
dataBean.setDate(allPostDateCount);
} else {
dataBean.setDate(null);
}
//設(shè)置作者信息
List<HashMap<String, String>> userMeta = userService.getUserMeta(1);
dataBean.setAuthor(userMeta);
homeBean.setData(dataBean);
homeBean.setCode(ResponseObj.OK);
homeBean.setMsg(ResponseList.OK_STR);
return new GsonUtils().toJson(homeBean);
} catch (Exception e) {
e.printStackTrace();
//查詢失敗
homeBean.setCode(ResponseObj.FAILED);
homeBean.setMsg(ResponseList.FAILED_STR);
return new GsonUtils().toJson(homeBean);
}
}
/**
* 根據(jù)作者的ID獲取作者的信息
*
* @param userId 作者ID
* @return 返回作者的json信息
*/
@RequestMapping(value = "/getAuthorInfo"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(
value = "獲取作者信息"
, notes = "獲取作者基本信息的json簇爆,具體字段請參照輸出的json數(shù)據(jù)"
, response = PostBean.class)
@ResponseBody
public Object getAuthorJson(int userId) {
ResponseObj<Object> responseObj = new ResponseObj<>();
try {
List<HashMap<String, String>> userMeta = userService.getUserMeta(userId);
if (null == userMeta || userMeta.isEmpty()) {
responseObj.setCode(ResponseObj.EMPUTY);
responseObj.setMsg(ResponseObj.EMPUTY_STR);
} else {
responseObj.setCode(ResponseObj.OK);
responseObj.setMsg(ResponseObj.OK_STR);
responseObj.setData(userMeta);
}
return new GsonUtils().toJson(responseObj);
} catch (Exception e) {
e.printStackTrace();
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg(ResponseObj.FAILED_STR);
return new GsonUtils().toJson(responseObj);
}
}
/**
* RESTful風(fēng)格的文章頁面
*
* @param postId 文章ID
* @return 返回文章頁面
*/
@RequestMapping(path = "/post/{postId}", method = RequestMethod.GET)
@ApiOperation(
value = "打開文章詳情web界面"
, notes = "文章詳情web界面,js模板加載網(wǎng)頁數(shù)據(jù)")
public ModelAndView getPostView(@PathVariable int postId) {
ModelAndView resultView = new ModelAndView("frontPost");
resultView.addObject("framJson", getFramJson());
resultView.addObject("postJson", getPostById(postId));
return resultView;
}
/**
* 根據(jù)文章ID獲取文章內(nèi)容
*
* @param postId 文章ID
* @return 返回文章ID對應(yīng)的文章內(nèi)容
*/
@RequestMapping(value = "/getPost"
, produces = {APPLICATION_JSON_UTF8_VALUE}
, method = {RequestMethod.GET, RequestMethod.POST})
@ApiOperation(
value = "根據(jù)id獲取文章json"
, notes = "根據(jù)文章的ID獲取文章的詳情json"
, response = PostBean.class)
@ResponseBody
public Object getPostById(
@ApiParam(value = "文章ID", required = true)
@RequestParam("postId")
int postId) {
ResponseObj<Object> responseObj = new ResponseObj<>();
try {
PostBean postBean = postService.findPostById(postId);
if (null == postBean) {
responseObj.setCode(ResponseObj.EMPUTY);
responseObj.setMsg(ResponseObj.EMPUTY_STR);
} else {
responseObj.setCode(ResponseObj.OK);
responseObj.setMsg(ResponseObj.OK_STR);
responseObj.setData(postBean);
}
return new GsonUtils().toJson(responseObj);
} catch (Exception e) {
e.printStackTrace();
responseObj.setCode(ResponseObj.FAILED);
responseObj.setMsg(ResponseObj.FAILED_STR);
return new GsonUtils().toJson(responseObj);
}
}
}
至此我們的Spring-Fox簡單實用已經(jīng)完成爽撒,后續(xù)的操作我們在需要的地方再查找資料就行了。
總結(jié)
本期項目都是簡單的介紹了一些東西响蓉,主要有:
- 登錄密碼校驗規(guī)則(MD5→SHA256)
- Spring-Fox的引入
- Spring-Fox在非springBoot中的使用
- Spring-Fox的使用
這一期本來是上一周就應(yīng)該完成的硕勿,但是回家懶癌發(fā)作又拖了一周,期間女朋友還各種生病枫甲,每天也沒心思寫代碼源武,很對不起大家了,后面我會更加努力想幻,也感謝一些哥們在我博客上面的留言鼓勵粱栖,謝謝大家。