Tag: spring mvc

如果我们在spring MVC中交换@service和@repository注释会发生什么

为什么我们需要在DAO实现中的服务实现和@repository中使用@service 。 当我在spring MVC中交换@service和@repository注释时没有问题。

将多个注释合并为一个以避免重复它们

我正在使用Spring MVC来实现各种REST服务。 对于文档,我使用的是Swagger。 这很好用,文档看起来很好,非常实用。 我唯一的问题是文档的注释真的挤满了控制器类,尤其是错误代码注释。 例: @ApiErrors(value = { @ApiError(code = 123, reason = “Reason123”), @ApiError(code = 124, reason = “Reason124”), @ApiError(code = 125, reason = “Reason125”), @ApiError(code = 126, reason = “Reason126”), @ApiError(code = 127, reason = “Reason127”) }) public void exampleFunctionImplementation() { } 在许多情况下,这会导致大量注释,其中真正的应用程序代码隐藏在其间的某处。 此外,这个注释集经常被重复,因为许多方法可能返回相同的错误代码集。 有没有选择通过将注释列表定义为其他类文件中的常量来缩短这一点? 或许我可能忽略了一些更简单的事情? 我尝试在某处定义@ApiError项的数组,但这不会编译: ApiError[] array = {ApiError(code = […]

如何使用java配置和没有Web.xml处理Spring MVC中的404页面未找到exception

我想在我的Spring MVC Web应用程序中处理404页面未找到exception,我正在使用SPRING 4.2.5.RELEASE ,我已经阅读了有关此主题的几个问题,但类似的问题是使用不同的spring java配置。 我有一个全局exception处理程序控制器类,它具有我的所有exception,这个类工作正常,但我无法处理404页面未找到exception。 这是我按照教程学习的方法 1)我创建了一个名为ResourceNotFoundException的类,它从RuntimeException扩展而来,我把这个注释放在了类定义@ResponseStatus(HttpStatus.NOT_FOUND) 像这样: @ResponseStatus(HttpStatus.NOT_FOUND) public class ResourceNotFoundException extends RuntimeException { } 2)我在exception的控制器类中创建了这个方法 @ExceptionHandler(ResourceNotFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public String handleResourceNotFoundException() { return “notFoundJSPPage”; } 但是,当我放置一个不存在的URL时,我收到此错误“找不到带HTTP的HTTP请求的映射” 我读过的问题说我需要启用Dispatcher的true选项,但由于我的配置与其他问题不同,我没有Web.xml所以我无法应用。 这是我的Config.java @EnableWebMvc @Configuration @ComponentScan({“config”, “controllers”}) public class ConfigMVC extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/resources/**”).addResourceLocations(“/WEB-INF/resources/”); } @Bean public UrlBasedViewResolver setupViewResolver() { UrlBasedViewResolver […]

空请求正文未被Spring @RequestBody @Valid注释捕获

我目前在Spring中遇到了@RequestBody注释的问题。 我目前在我的模型上正确设置了所有validation注释,并且在对象发布时它们工作得很好。 即使发布的请求主体完全为空或空对象“{}”,一切都按预期工作。 当有人试图发布“null”请求主体时,就会出现问题。 这以某种方式通过@Valid注释获取并且没有被捕获,当我尝试访问该对象时导致NullPointerException 。 我在下面粘贴了我的控制器片段。 @Secured({ ROLE_ADMIN }) @RequestMapping(method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE } ) public HttpEntity<Resource> addItem(@RequestBody @Valid Item item) throws Exception { Resource itemResource = this.itemResourceAssembler.toResource(this.itemService.save(item)); return new ResponseEntity<Resource>(itemResource, HttpStatus.CREATED); } 我在itemService.save()做了一些检查,看看数据库中是否存在一个项目,因为它被用作upsert。 当我访问传递给save的项时,我得到NullPointerException因为item为null。 我已经尝试使用@RequestBody的“required”参数,但只检查是否有POST的内容。 如上所述,只有当用户在没有引号作为请求主体的情况下发布“null”时,才会传递空项。 是否有自动的Spring注释或配置来阻止此null被传递,或者我应该只是把if(item == null) throw new Exception(); 在我的所有控制器中,这可能会出现?

Spring XSD的访问错误

我正在尝试在我的工作中运行一名前雇员留下的系统,但我遇到了问题。 如果XSD通过远程访问运行: 它给出了一个未找到的exception: Failed to read schema document ‘http://www.springframework.org/schema/context/spring-context-3.0.xsd’, because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not . 如果XSD通过本地访问运行: 它给出了这个例外: C:\Users\claudiomazur>java -jar c:\temp\fin\c.jar 0 [AWT-EventQueue-0] INFO support.ClassPathXmlApplicationContext – Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4fa52fdf: startup date [Thu Sep 06 11:22:59 BRT 2012]; root of […]

spring警告:不支持请求方法“HEAD”

在tomcat 7上成功部署后,我每秒都会收到警告消息: org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported警告:不支持请求方法’HEAD’ 但应用程序工作。 如何避免这个恼人的消息?

在Spring Controller中获取Locale的优雅方法

我正在寻找一种更清晰的方式(在Spring 3.2中)获取当前语言环境,而不是在每个Controller方法的开头显式调用LocaleContextHolder.getLocale()。 它必须与Java注释兼容,因为我没有使用XML配置。 这就是我目前正在做的事情。 @Controller public class WifeController { @Autowired private MessageSource msgSrc; @RequestMapping(value = “/wife/mood”) public String readWife(Model model, @RequestParam(“whatImDoing”) String iAm) { Locale loc = LocaleContextHolder.getLocale(); if(iAm.equals(“playingXbox”)) { model.addAttribute( “statusTitle”, msgSrc.getMessage(“mood.angry”, null, loc) ); model.addAttribute( “statusDetail”, msgSrc.getMessage(“mood.angry.xboxdiatribe”, null, loc) ); } return “moodResult”; } }

如何获取默认的WebApplicationContext?

我需要ApplicationContext.xml的上下文,我在web.xml中提供了它 org.springframework.web.context.ContextLoaderListener 但是我需要在Controller类中Controller 。 我尝试了很多东西,包括 WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext(); 但它没有帮助。

在Spring Boot测试类上使用@WebMvcTest批注时出错

我已经按照这里描述的“测试Spring MVC切片”​​部分为Spring MVC控制器编写了一个测试类。 这个类看起来像这样: @RunWith(SpringRunner.class) @WebMvcTest(controllers=OurController.class) public class OurControllerTest { @Autowired private MockMvc mvc; @MockBean private OurRepository ourRepository; @Test public void testGetStuff() { // code removed } } 当我运行它时,我收到以下错误: MockHttpServletResponse: Status = 401 Error message = null Headers = {Cache-Control=[no-store], Pragma=[no-cache], WWW-Authenticate=[Bearer realm=”oauth2-resource”, error=”unauthorized”, error_description=”Full authentication is required to access this resource”], Content-Type=[application/json;charset=UTF-8], X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; […]

抓住spring初始化所有豆子的时刻

我有春季申请(我没有懒豆)。 我想在初始化所有@Component(@Repositoey @Service @Controller)bean时插入逻辑。 我该怎么做?