当前位置: 首页 > news >正文

建站网址不安全个人网站建设简历

建站网址不安全,个人网站建设简历,网站被抄袭怎么办,佛山建设网站公司在Spring 4的许多新功能中#xff0c;我发现了ControllerAdvice的改进。 ControllerAdvice是Component的特殊化#xff0c;用于定义适用于所有RequestMapping方法的 ExceptionHandler#xff0c; InitBinder和ModelAttribute方法。 在Spring 4之前#xff0c; ControllerAd… 在Spring 4的许多新功能中我发现了ControllerAdvice的改进。 ControllerAdvice是Component的特殊化用于定义适用于所有RequestMapping方法的 ExceptionHandler InitBinder和ModelAttribute方法。 在Spring 4之前 ControllerAdvice在同一Dispatcher Servlet中协助了所有控制器。 在Spring 4中它已经发生了变化。 从Spring 4开始可以将ControllerAdvice配置为支持定义的控制器子集而仍可以使用默认行为。 ControllerAdvice协助所有控制器 假设我们要创建一个错误处理程序它将向用户显示应用程序错误。 我们假设这是一个基本的Spring MVC应用程序其中Thymeleaf作为视图引擎并且我们有一个ArticleController它具有以下RequestMapping方法 package pl.codeleak.t.articles;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping;Controller RequestMapping(article) class ArticleController {RequestMapping({articleId})String getArticle(PathVariable Long articleId) {throw new IllegalArgumentException(Getting article problem.);} } 如我们所见我们的方法抛出了一个假想异常。 现在使用ControllerAdvice创建一个异常处理程序。 这不仅是Spring中处理异常的可能方法。 package pl.codeleak.t.support.web.error;import com.google.common.base.Throwables; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.ModelAndView;ControllerAdvice class ExceptionHandlerAdvice {ExceptionHandler(value Exception.class)public ModelAndView exception(Exception exception, WebRequest request) {ModelAndView modelAndView new ModelAndView(error/general);modelAndView.addObject(errorMessage, Throwables.getRootCause(exception));return modelAndView;} } 该课程不是公开的不是公开的。 我们添加了ExceptionHandler方法该方法将处理所有类型的Exception并将返回“错误/常规”视图 !DOCTYPE html html xmlnshttp://www.w3.org/1999/xhtml xmlns:thhttp://www.thymeleaf.org headtitleError page/titlemeta http-equivContent-Type contenttext/html; charsetUTF-8/link href../../../resources/css/bootstrap.min.css relstylesheet mediascreen th:href{/resources/css/bootstrap.min.css}/link href../../../resources/css/core.css relstylesheet mediascreen th:href{/resources/css/core.css}/ /head body div classcontainer th:fragmentcontentdiv th:replacefragments/alert :: alert (typedanger, message${errorMessage}) /div /div /body /html 为了测试解决方案我们可以运行服务器或者最好使用Spring MVC Test模块创建一个测试。 由于我们使用Thymeleaf因此可以验证渲染的视图 RunWith(SpringJUnit4ClassRunner.class) WebAppConfiguration ContextConfiguration(classes {RootConfig.class, WebMvcConfig.class}) ActiveProfiles(test) public class ErrorHandlingIntegrationTest {Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;Beforepublic void before() {this.mockMvc webAppContextSetup(this.wac).build();}Testpublic void shouldReturnErrorView() throws Exception {mockMvc.perform(get(/article/1)).andDo(print()).andExpect(content().contentType(text/html;charsetISO-8859-1)).andExpect(content().string(containsString(java.lang.IllegalArgumentException: Getting article problem.)));} } 我们期望内容类型为text / html并且视图包含带有错误消息HTML片段。 但是它并不是真正的用户友好型。 但是测试是绿色的。 使用上述解决方案我们提供了一种处理所有控制器错误的通用机制。 如前所述我们可以使用ControllerAdvice做更多的事情。 例如 ControllerAdvice class Advice {ModelAttributepublic void addAttributes(Model model) {model.addAttribute(attr1, value1);model.addAttribute(attr2, value2);}InitBinderpublic void initBinder(WebDataBinder webDataBinder) {webDataBinder.setBindEmptyMultipartFiles(false);} }ControllerAdvice协助选定的控制器子集 从Spring 4开始可以通过批注basePackageClassesbasePackages方法来自定义ControllerAdvice以选择控制器的一个子集来提供帮助。 我将演示一个简单的案例说明如何利用此新功能。 假设我们要添加一个API以通过JSON公开文章。 因此我们可以定义一个新的控制器如下所示 Controller RequestMapping(/api/article) class ArticleApiController {RequestMapping(value {articleId}, produces application/json)ResponseStatus(value HttpStatus.OK)ResponseBodyArticle getArticle(PathVariable Long articleId) {throw new IllegalArgumentException([API] Getting article problem.);} } 我们的控制器不是很复杂。 如ResponseBody批注所示它返回Article作为响应正文。 当然我们要处理异常。 而且我们不希望以text / html的形式返回错误而是以application / json的形式返回错误。 然后创建一个测试 RunWith(SpringJUnit4ClassRunner.class) WebAppConfiguration ContextConfiguration(classes {RootConfig.class, WebMvcConfig.class}) ActiveProfiles(test) public class ErrorHandlingIntegrationTest {Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;Beforepublic void before() {this.mockMvc webAppContextSetup(this.wac).build();}Testpublic void shouldReturnErrorJson() throws Exception {mockMvc.perform(get(/api/article/1)).andDo(print()).andExpect(status().isInternalServerError()).andExpect(content().contentType(application/json)).andExpect(content().string(containsString({\errorMessage\:\[API] Getting article problem.\})));} } 测试是红色的。 我们能做些什么使其绿色 我们需要提出另一个建议这次仅针对我们的Api控制器。 为此我们将使用ControllerAdvice注解选择器。 为此我们需要创建一个客户或使用现有注释。 我们将使用RestController预定义注释。 带有RestController注释的控制器默认情况下采用ResponseBody语义。 我们可以通过将Controller替换为RestController并从处理程序的方法中删除ResponseBody来修改我们的控制器 RestController RequestMapping(/api/article) class ArticleApiController {RequestMapping(value {articleId}, produces application/json)ResponseStatus(value HttpStatus.OK)Article getArticle(PathVariable Long articleId) {throw new IllegalArgumentException([API] Getting article problem.);} } 我们还需要创建另一个将返回ApiError简单POJO的建议 ControllerAdvice(annotations RestController.class) class ApiExceptionHandlerAdvice {/*** Handle exceptions thrown by handlers.*/ExceptionHandler(value Exception.class)ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)ResponseBodypublic ApiError exception(Exception exception, WebRequest request) {return new ApiError(Throwables.getRootCause(exception).getMessage());} } 这次运行测试套件时两个测试均为绿色这意味着ExceptionHandlerAdvice辅助了“标准” ArticleController而ApiExceptionHandlerAdvice辅助了ArticleApiController。 摘要 在以上场景中我演示了如何轻松利用ControllerAdvice批注的新配置功能希望您像我一样喜欢所做的更改。 参考文献 SPR-10222 RequestAdvice注释文档 参考 ControllerAdvice在我们Spring的JCG合作伙伴 Rafal Borowiec的Codeleak.pl博客上的改进 。 翻译自: https://www.javacodegeeks.com/2013/11/controlleradvice-improvements-in-spring-4.html
http://www.huolong8.cn/news/176526/

相关文章:

  • 淘客商品网站怎么做的自考本科需要什么条件
  • 计算机网络技术 网站建设方向网站二级页怎么做
  • 电商网站开发思路模版网站建设的实训心得 500字
  • 公司网站域名在哪里备案长沙在线建站模板
  • 手机网站源码3免费做网站
  • 请人做网站设计的方案谷歌seo和百度seo区别
  • 网站投票页面怎么做怎么建网站赚钱
  • 技术网站建设买软件网站建设
  • 怎么做免费网站推广中山建设工程有限公司
  • 广州市建设企业网站平台阜阳市网站建设
  • 网站的系统帮助网站被篡改处理
  • 做百度移动网站点击wordpress最新文章链接插件
  • 苏州网站建设制作设计网站建设 多少钱
  • 网站开发网页超链接路径物流公司排名前十
  • 企业做淘宝网站需要多少钱信用中国企业查询
  • 有经验的邵阳网站建设怎么做微信辅助的网站
  • 网站推广工作如何做广州建设工程造价管理站
  • 动易视频网站管理系统上海网站微信平台建设
  • 中学院新校区建设专题网站做一家开发网站的公司
  • 专业的网站建设找聚爱长沙专业做网站公司
  • 常用的网站制作如何电话推销客户做网站
  • 家庭宽带做网站服务器小型门户网站有哪些
  • 青岛做视频的网站百度指数名词解释
  • 网站毕业设计开题报告山西网站建设运营公司
  • 陕西省住房和城市建设厅网站php网站开发学校
  • 广州建设执业资格注册中心网站seo排名如何
  • c 网站开发需要学什么玉环 企业网站建设
  • 手机版网站开发实例推荐一个做照片书的网站
  • ip详细地址查询工具佛山seo技术
  • 口碑好的盐城网站建设wordpress备份图文文章