Spring MVC hashmap model



I'm working on an existing project and they are using SPRING framework. Just want to ask if it's a good design to have lot of Hashmaps as MODEL in your CONTROLLER rather than utilizing pojos and beans ?


Related to : Spring MVC hashmap model
Multipart File Upload Using Spring Rest Template + Spring Web MVC
Programming Languages

I am trying to upload a File using RestTemplate with the following code.

   MultiValueMap<String, Object> multipartMap = new
LinkedMultiValueMap<>();
   multipartMap.add("file", new ClassPathResource(file));

   HttpHeaders headers = new HttpHeaders();
   headers.setContentType(new MediaType("multipart", "form-data"));

   HttpEntity<MultiValueMap<String, Object>> request = new
HttpEntity<MultiValueMap<String, Object>>(multipartMap,
headers);

   System.out.println("Request for File Upload : " + request);

   ResponseEntity<byte[]> result = template.get().exchange(
                    contextPath.get() + path, HttpMethod.POST,
request,
                    byte[].class);

I have the MultipartResolver bean and Controller code is

@RequestMapping(value = "/{id}/image", method =
RequestMethod.POST)
@ResponseStatus(HttpStatus.NO_CONTENT)
@Transactional(rollbackFor = Exception.class)
public byte[] setImage(@PathVariable("id") Long userId,
        @RequestParam("file") MultipartFile file) throws IOException {
    // Upload logic
}

And I get the following Exception


org.springframework.web.bind.MissingServletRequestParameterException:
Required MultipartFile parameter 'file' is not present
        at
org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:255)
~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:95)
~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
~[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
[spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:80)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:67)
[log4j-web-2.0.2.jar:2.0.2]
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
m.m.m.AbstractPreAuthenticatedProcessingFilter.doFilter(UapAbstractPreAuthenticatedProcessingFilter.java:109)
[classes/:?]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
        at
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
[spring-web-4.0.6.RELEASE.jar:4.0.6.RELEASE]
        at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
[?:1.7.0_67]
        at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
[?:1.7.0_67]
        at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
[tomcat-embed-core-7.0.54.jar:7.0.54]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_67]

Why Spring MVC does not allow to expose Model or BindingResult to an @ExceptionHandler?
Programming Languages

Situation

I'm trying to group the code that logs the exceptions and render a nice view in a few methods. At the moment the logic is sometime in the @RequestHandler itself (in the a catch block), othertimes is delegated to an utility class (that works but moves the logic away from the place where the exception is thrown).

Spring's @ExceptionHandler seemed the way to group everything in one place (the controller itself or a parent) and get rid of some code (no need to put logic in the try-catch and no need for an utility class)... until I realized that an @ExceptionHandler methods won't have the ModelMap or BindingResult parameters autowired. Currently those objects are used to render the view with a sensible error message and we want to log some information contained in these objects as well.

Question

Why Spring doesn't not support method arguments such ModelMap or BindingResult for the @ExceptionHandler? What is the rationale behind it?

Possible solution

In the Spring source code (3.0.5) the arguments for the method are resolved in the HandlerMethodInvoker.invokeHandlerMethod. A an exception thrown by the request handler is caught there and re-thrown. The @ExceptionHandler and it's parameters are resolved elsewhere. As a workaround I thought to check if the Exception implements an hypothetical "ModelAware" or "BindingResultAware" interface, and in that case set Model and BindingResult attributes before re-throwhing it. How does it sound?


How to configure single thread model in spring MVC framework?
Programming Languages

In my MVC framework,I have a controller class. That should be accessed by only one thread at a time. My class should not implement SingleThreadModel interface or should not use synchronized blocks. How can I achieve this?

It seems there is a configuration available in web.xml. Using a tag we can achieve this.Is there any web.xml configuraiton?


Spring Security, Spring MVC, PreAuthorise annotation and exception handling
Programming Languages

I'm struggling with Spring Security and exception handling.

I have the following exception handler configured in my servlet-context:

   <bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView"
value="/errors/general-error"/>
        <property name="defaultStatusCode" value="500" />
        <property name="warnLogCategory"
value="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"/>
    </bean>

This catches any uncaught exceptions, and shows a general error page.

I also have Spring Security configured using a CAS single sign on server. If in my Security configuration I have the following:

<security:intercept-url pattern="/secure/**"
access="hasRole('USER')" />

If an unauthenticated user visits "/secure", they are correctly redirected to login, then they return and can view the page.

I'd prefer to use PreAuthorise() annotations on the MVC methods, rather than having to define the URLs manually. So instead, if I have the following annotation:

    @PreAuthorize("hasRole('USER')")
    @RequestMapping("/secure")
    public String displaySecurePage(
...
}

The redirect to the CAS SSO server doesn't happen, and Spring simply displays the general error page, with an Exception org.springframework.security.access.AccessDeniedException: Access is denied

Why is there a difference between the two ways of defining the access permissions, and how can I use the PreAuthorise annotation with Spring correctly directing an unauthenticated user to login, and still have a exception handler to catch other exceptions?


CSRF Token coming empty in login form using Spring MVC and Spring Security
Programming Languages

${_csrf.parameterName} and ${_csrf.token} coming empty in login form. I am using Spring 4.1.3 and Spring Security 3.2.5
All the configurations are made correctly but still i am getting

<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}"/>

as

<input type="hidden" name="" value=""/>

Spring MVC hashmap model
Programming Languages

I'm working on an existing project and they are using SPRING framework. Just want to ask if it's a good design to have lot of Hashmaps as MODEL in your CONTROLLER rather than utilizing pojos and beans ?



Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .