I am trying to make simple rest service which is used by everybody example it will consume by mobile developer.so I need to send static data to every one .I am trying to send static this data .
{
name:"abcd"
}
in other word if some one hit my system like this
http://192.168.12.61:8080/springfirst/hello .then user get above json.
I follow this like to make
http://www.programming-free.com/2014/03/spring-mvc-40-restful-web-service-json.html
I follow this step
download these jar files(-- jackson-annotations-x.x.x.jar
-- jackson-core-x.x.x.jar
-- jackson-databind-x.x.x.jar) and include in lib folder.
here is my code
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
hello-servelts.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tutorialspoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
controller.js
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/hello")
public class HelloController{
#RequestMapping( method = RequestMethod.GET,headers="Accept=application/json")
public String printHello(ModelMap model) {
return "abcd";
}
}
you have configuration problems:
If you register DispatcherServlet in web.xml with out context configuration file path, then you should name the context file as per your servletName-servlet.xml.
So rename hello-servelts.xml as HelloWeb-servlet.xml.
and add #ResponseBody in your controller handler method to return JSON like:
#RequestMapping( method = RequestMethod.GET,headers="Accept=application/json")
public #ResponseBody Map printHello(ModelMap model) {
Map<String,String> json = new HashMap<String,String>();
json.put("name", "abcd");
return json;
}
Here is the working application using ContentNegotiatingViewResolver.
how to make rest service in spring mvc?
Ans, there is different ways are available. I am listing below some of:
To read/write JSON data from HTTP request or response you should use #RequestBody to read from HTTP request and #ResponseBody to write a object as JSON into HTTP response.
Spring provides ContentNegotiatingViewResolver where you can use it to resolve Views by request URL extension OR request ACCEPT header value. for example if URL is /view.html then it will return a view which has text/html content-type. same you can configure it to return JSON as well.
ContentNegotiatingViewResolver configuration for JSON View will look like:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
Note: Jackson mapper or any other mapper should be available on buildpath in order to work JSON serialize and deserialize.
If you use Maven, then confirm this dependency available in pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
SEE ALSO:
How to return object from controller to ajax in spring-mvc
New features in spring mvc 3.1
You can follow this guide which is an official documentation and it is using spring-boot which will do easy your way to start writing services.
You rest service would be something like
#RestController
#RequestMapping("/hello")
public class HelloController{
#RequestMapping( method = RequestMethod.GET,headers="Accept=application/json")
#ResponseBody
public String printHello() {
return "abcd";
}
}
Related
I'm building a new project for SOAP web services. Earlier I was using JDBC layer for opening and closing connections. Now, I'm converting it into Spring with JDBC template. I've configured all the layers and annotated the components. When I try to use the dao bean in my service impl class, it throws me null pointer exceptions
#Service
#WebService
public interface Transaction { // Web methods here for SOAP Web service
}
Impl class
#Component
#WebService
public class TransactionImpl implements Transaction{
#Autowired
BBDao dao; --> This is coming as null when I use it in the method
}
BBDao interface is as follows
public interface BBDao { /* Methods in it */ }
The implementation class which is implementing BBDao interface is
public class BBDaoImpl extends JdbcDaoSupport implements BBDao {
#Autowired
ServletContext ctx;
#Autowired
DataSource dataSource;
// Methods overriding here
}
Servlet defined in web.xml
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And finally the spring-web-servlet.xml is
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.bb.controller,com.bb.dao,com.bb.service" />
<context:property-placeholder location="classpath:datasource-cfg.properties" />
<bean id="bbDAO" class="net.bb.dao.BBDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- AS400 Data source Bean -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.as400.access.AS400JDBCDriver" />
<property name="url" value="${as400.url}" />
<property name="username" value="${as400.username}" />
<property name="password" value="${as400.password}" />
</bean>
<mvc:annotation-driven />
</beans>
The BBDao bean object is coming as null.
Is there any mistake in my configuration? Any advice would be greatly appreciated.
P.S : I have followed other posts as well, as most of the posts talk about component scan only and the packages are correct
We cant autowire an interface without Implementation as we cant create instance for interface. But you can try the ones below and see if it works.
Few things what you could do.
Add to your spring-webservlet.xml
And also refer this
user annotation like #ConditionalOnMissingBean(BBDao.class)
I've been experimenting with Java Servlets for web applications, in this application I was able to hit a Servlet and correctly load a .jsp page, having done this I have moved onto Spring MVC. I've run into a problem where my servlet controller class is called, however it will not load the view.
I've ruled out an the resources not being visible, because it worked correctly with a plain java servlet. I've also read just about every resource/tutorial out there in an effort to attempt to identify the problem without any luck, my problem remains the same. In addition in an effort to trouble-shoot I've added an error page tag () in order to see if when I attempt to hit my page, it would correctly redirect me, but its unable to find the page specified for 404 errors.
Can anyone identify what I've missed?
Web.xml
Variations: Changed url-pattern, init-params, context config location etc.
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/LoginServlet-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
LoginServlet-servlet.xml
Variations: I've tried moving the declarations into different positions as has been suggested on other posts, to no result. In addition typically I've had the prefix set to /WEB-INF/jsp/
<context:component-scan base-package="plan.route.server" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<context:annotation-config/>
<mvc:annotation-driven />
LoginServlet.java
Variations: Different requestMapping path, marking the methods not the class, returning string from methods, returning ModelAndView class
package plan.route.server;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller()
#RequestMapping("/")
public class LoginServlet extends org.springframework.web.servlet.mvc.AbstractController {
#RequestMapping(method = RequestMethod.GET)
public String forwardTo() {
return "index";
}
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception {
return new ModelAndView("login", "login", "login");
}
}
Project setup
Variations: different locations for the servlet xml, .jsp files etc
Can anyone see what I've missed? all I'm trying to do, despite all the variations is load a .jsp page.
Edit: The following error is displayed after my java servlet method is called:
WARNING: No mapping found for HTTP request with URI [/Root/Login] in DispatcherServlet with name 'LoginServlet'
I see one thing that is wrong and is the jsp configuration in LoginServlet-servlet.xml, try change prefix value as follows:
<context:component-scan base-package="plan.route.server" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<context:annotation-config/>
<mvc:annotation-driven />
With your configuration Spring is not able to find jsp file, because you specified the wrong path. You have to be folder specific, in your case you have jsp files in /WEB-INF/jsp folder.
EDIT:
I configured your project in my workspace and it works. Try to remove this lines from web.xml:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/LoginServlet-servlet.xml</param-value>
</init-param>
And your Controller class should be like this:
#Controller
#RequestMapping("/")
public class LoginServlet{
#RequestMapping(method = RequestMethod.GET)
public ModelAndView forwardTo(ModelMap model) {
return new ModelAndView("login", "login", "login");
}
}
And pay attention on how you invoke the controller:
http://localhost:8080/Root/
This is the correct way to call the controller because you named your project Root and the controller is listening to "/" path. I used port 8080 because you tagged the question with tomcat, and this is the default tomcat port, if you use another one change it with the one you use.
In LoginServlet-servlet.xml file try
<property name="prefix" value="/WEB-INF/jsp/"/>
instead of
<property name="prefix" value="/" />
With your current project setup
LoginServlet-servlet.xml
<context:component-scan base-package="plan.route.server" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<context:annotation-config/>
<mvc:annotation-driven />
LoginServlet.java
#Controller
#RequestMapping("/")
public class LoginServlet {
#RequestMapping(method = RequestMethod.GET)
public String forwardTo() {
return "index";
}
#RequestMapping(value="/login", method = RequestMethod.GET)
public String forwardToLogin() {
return "login";
}
}
This should work
I am having issue in spring3 frameowrk.org.springframework.web.servlet.DispatcherServlet
file name is WelcomeController.java
package com.rethink.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/welcome")
public class WelcomeController {
#RequestMapping(method = RequestMethod.GET)
public String displayMessage(Map<String, String> map){
System.out.println("In");
map.put("loginmessage","Please Login with Your Details");
return "index";
}
}
this is my web.xml and motion-comics-servlet.xml
Click here for web.xml and motion-comics-servlet.xml screen shot
I have placed my view in Web Pages/Web-INF/jsp/index.jsp
no matter what i try i just can't make it to work....
my help and advise will be much appreciated ....
thanks in advance
I tried some examples and found out couple of configuration changes are required in your code.
Add < mvc:annotation-driven /> to your motion-comics-servlet.xml file.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Add this to your xml file -->
<mvc:annotation-driven/>
<!-- Make sure that you are scanning all your packages required for your application -->
<context:component-scan base-package="com.rethink.controller"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Change motion-comics servlet-mapping to the following configuration. Change '/*' to '/'. Remove *.
<servlet-mapping>
<servlet-name>motion-comcis</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Please let me know if you run into any issues.
You can verify the spring web mvc logs, whether the controller is getting registered for that URL or not. Also verify whether the component scan is properly set or not.
Further you can verify the view resolver is configured appropriately or not.
For more detailed understanding check this post
There is a small Struts application and I am trying to enable Spring-mvc on it. It's already using Spring to handle the DB transactions. I have two questions:
Component scan will not pick up my controllers if I try to add a new base package.
If I place my controllers in an existing base package, I can see them created in Spring application context. But then the request mapping still does not work.
Here are my relevant code snippets:
web.xml:
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.site</url-pattern>
</servlet-mapping>
Here is springDispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.mycom.eps.test, com.mycom.epsadmin"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="exceptionHandler" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">error</prop>
<prop key="org.hibernate.exception.GenericJDBCException">jdbcerror</prop>
</props>
</property>
</bean>
</beans>
Here's part of ApplicationContext.xml:
<context:annotation-config />
<context:component-scan base-package="com.mycom.eps, com.mycom.tiff" />
<tx:annotation-driven proxy-target-class="true" />
I've got two Controllers:
package com.mycom.eps.test;
// import statements
#Controller
public class TestController {
#RequestMapping(value="/test", method={RequestMethod.GET, RequestMethod.POST})
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws EpsException {
Map<String, Object> modelMap = new HashMap<String, Object>();
return new ModelAndView("/details", modelMap);
}
}
Another controller:
//This is a new package I am trying to create
package com.mycom.epsadmin.controller;
// import statements
#Controller
public class PackageController {
#RequestMapping(value="/sendPackage", method={RequestMethod.GET, RequestMethod.POST})
public ModelAndView sendPackage(HttpServletRequest request, HttpServletResponse response) throws EpsException {
Map<String, Object> modelMap = new HashMap<String, Object>();
return new ModelAndView("/details", modelMap);
}
}
My first question is, why is the com.mycom.epsadmin.controller.PackageController never created in web application context (I inspected the spring log and couldn't find it)?
While trying to figure out about my first question, I created another controller com.mycom.eps.test.TestController (hence the name of the controller). While this one does get created in the Web application context, the request is never intercepted (404 error).
Here's how I am trying to call it:
$.ajax({
type: "POST",
url: "test.site",
cache: false
});
When I try to go to the page through browser http://http://localhost:8080/mycom/test.site, I am getting 404 error as well.
Sorry for the lengthy post! But can someone kindly point me in the right direction? Thanks a bunch!
UPDATE:
Just found out that the test controller is actually picking up the request (really sorry about that)! So my second question is moot.
Try after adding com.mycom.epsadmin in the ApplicationContext
<context:component-scan base-package="com.mycom.epsadmin, com.mycom.eps, com.mycom.tiff" />
I'm trying to upload a multipart file using Ajax, Spring MVC 3.2.0, Tomcat 8.0.9, but can't get it work. I read a lot of blogs and similar posting here on stackoverflow (Spring upload file problems, MultipartConfig with Servlet 3.0 on Spring MVC, …) which seem to have similar causes but couldn't figure out how to solve it. The weird thing is that the upload works when the file is smaller than 1MB, but when ever the recorded video exceeds that size, the following error is raised:
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:163)
org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:110)
In the following you can see all the configurations I made:
The AJAX POST-Request:
var videoBlob = e.data;
var pathArray = window.location.pathname.split( '/' );
var userID;
for (i = 0; i < pathArray.length; i++) {
if (pathArray[i].toString() == "edit"){
userID = pathArray[i+1];
}
}
var fd = new FormData();
fd.append('fname', 'video');
fd.append('data', videoBlob);
$.ajax({
url: '/user/edit/uploadVideo/' + userID,
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data)
{
$('#result').html(data + "uploaded by FormData!");
}
});
The web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:root-context.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>common</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<display-name>springMultipartFilter</display-name>
<filter-name>springMultipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>springMultipartFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The servlet-context.xml
<mvc:annotation-driven />
<mvc:resources mapping="/**" location="/resources/" />
<context:component-scan base-package="de.talentwuerfel"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/schema"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>de.talentwuerfel</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
The root-context.xml where I defined the MultipartResolver
<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000"/>
<property name="maxInMemorySize" value="4096"/>
</bean>
The Java-Controller
#RequestMapping(value = "/edit/uploadVideo/{id}", method = RequestMethod.POST)
public #ResponseBody String uploadVideo(#PathVariable long id, MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
//.... file handling
}
How can I solve this problem?
EDIT:
I tried the suggested approach and used the Servlet implementation to manage my video-file upload. The following adjustments have been made, but it's still resulting in a similar error:
Adjusted #Controller:
#RequestMapping(value = "/edit/uploadVideo/{id}", method = RequestMethod.POST)
public String uploadVideo(#PathVariable long id, #RequestParam("data") Part file) {
//...
}
The root-controller has been deleted and I added the multipartResolver to the servlet-context.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
The tag was in the web.xml has been extended by the following Multipart-Configuration:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<multipart-config>
<location>/tmp</location>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
However, I'm still getting an exception and can't upload a blob file larger than 1MB:
Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
I implemented a similar file upload where a single file was simply picked and it totally worked to send large files while using the same configuration. So I believe it has rather something to do with the Ajax POST or the attached blob file?!
you can add this in you application to active Servlet3.0 MultiParsing:
#Bean
public MultipartConfigElement multipartConfigElement() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}
or do it in XML.
Not really an answer to your exact question, just my 2 cents. There are basically 2 ways of uploading files with the help of Spring MVC :
using Jakarta Commons FileUpload, the only way (aside from implementing one yourself) before the appearance of the servlet 3.0 API
using the Servlet implementation of your server (only if servlet impl version >= 3.0)
Since you are using Tomcat 8.0.9, the Servet 3.0 option is available to you which I definitely recommend since it doesn't introduce yet another external dependency in your project. Also, since it follows the Servlet 3.0 spec, the configuration of such upload mechanism is now java standard which is nice in case you decide to move from Spring MVC to another MVC framework (your configuration values would remain the same).
In case you can't figure out your IOFileUploadException, I think you should give it a try.