一丶swagger中的@Api注解
@Controller
@Api(tags="登录")
public class LoginController {}
在配置Swagger后,加入@Api注解后发现接口点击没反应,在网上查了一下,发现是在一些Swagger版本中tags属性无法解析中文。但框架整合后,Swagger容易跟Spring版本冲突,现在修改版本可能会让我的框架报错,得不偿失,就没有改。
二丶接口返回值未写为json形式
在进行前后端交互的时候,每个接口的返回值基本上都要以一个Json形式返回,方便前端的使用,而有的接口如果没有以Json的形式返回,就会报错!报错信息如下。
<!doctype html><html lang="zh"><head><title>HTTP状态 500 - 内部服务器错误</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP状态 500 - 内部服务器错误</h1><hr class="line" /><p><b>类型</b> 异常报告</p><p><b>消息</b> Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/pages/updateusermessage.html]")</p><p><b>描述</b> 服务器遇到一个意外的情况,阻止它完成请求。</p><p><b>例外情况</b></p><pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/pages/updateusermessage.html]")
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920)
javax.servlet.http.HttpServlet.service(HttpServlet.java:684)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
</pre><p><b>根本原因。</b></p><pre>org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/pages/updateusermessage.html]")
org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:235)
org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:649)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)
org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1401)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1145)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1084)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920)
javax.servlet.http.HttpServlet.service(HttpServlet.java:684)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
</pre><p><b>根本原因。</b></p><pre>java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/pages/updateusermessage.html]
org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159)
org.thymeleaf.spring5.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103)
org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223)
org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:649)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)
org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1401)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1145)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1084)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920)
javax.servlet.http.HttpServlet.service(HttpServlet.java:684)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
接口的代码如下
@ApiOperation(value = "修改个人信息")
@RequestMapping(value = "/updateusermessage", method = RequestMethod.PUT)
public
// @ResponseBody ModelAndView
void updateUserMessage(String sex, String nickname, String profile, String address, Integer id) {
User user = new User();
user.setSex(sex);
user.setNickname(nickname);
user.setProfile(profile);
user.setAddress(address);
user.setId(id);
userService.updateUser(user);
Map<String, Boolean> map = new HashMap<>();
map.put("result", true);
// return new ModelAndView(new MappingJackson2JsonView(), map);
}
三丶Swagger在测试的时候缓冲个不停,没有任何报错信息
有的时候在Swagger测试接口的时候,它会转个不停,进不去方法,没有任何报错,让人无从下手,最后我发现是数据库连接出了问题,因为之前服务器还没建立好,Push了一次代码后我数据库配置文件中密码被覆盖,跟我数据库密码不同,致使连接不上数据库,取不出来数据。