I am using Spring MVC with JSON as specified in Ajax Simplification Spring 3.0 article.
After so many attempts and variations of my code depending on advice found on
I have used java configuration and i got this same error. I have missed to add the @EnableWebMvc to the configuration file. This error is resolved after i add the @EnableWebMvc in my webconfig file.
Also the Object that is returned from your Spring Controller, should have proper getter and setter methods.
package com.raghu.dashboard.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.raghu.dashboard.dao.ITaskDAO;
import com.raghu.dashboard.dao.TaskDAOImpl;
@Configuration
@EnableWebMvc //missed earlier...after adding this it works.no 406 error
@ComponentScan(basePackages = { "com.raghu.dashboard.api", "com.raghu.dashboard.dao" })
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class>[] getRootConfigClasses() { return null;}
protected Class>[] getServletConfigClasses() {
return new Class[] { MongoConfiguration.class};
}
protected String[] getServletMappings() {
return new String[]{"*.htm"};
}
@Bean(name = "taskDao")
public ITaskDAO taskDao() {
return new TaskDAOImpl();
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
AppInitializer.java
package com.raghu.dashboard.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class AppInitalizer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(com.raghu.dashboard.config.WebConfig.class);
context.scan("com.raghu.dashboard.api");
return context;
}
}
Also make sure the Object that is returned, has the proper getter and setter.
Example:
@RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity findAll() {
logger.info("Calling the findAll1()");
TaskInfo taskInfo = dashboardService.getTasks();
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
ResponseEntity entity = new ResponseEntity(taskInfo,
headers, HttpStatus.OK);
logger.info("entity is := " + entity);
return entity;
}
TaskInfo object should have proper getter and setter. if not, 406 error will be thrown.
POM File for Reference:
4.0.0
com.raghu.DashBoardService
DashBoardService
war
0.0.1-SNAPSHOT
DashBoardService Maven Webapp
http://maven.apache.org
4.0.6.RELEASE
2.4.0
2.2.11
1.2.17
junit
junit
3.8.1
test
org.mongodb
mongo-java-driver
2.10.1
org.springframework.data
spring-data-mongodb
1.4.1.RELEASE
org.springframework
spring-beans
${spring-framework.version}
org.springframework
spring-core
${spring-framework.version}
org.springframework
spring-context
${spring-framework.version}
org.springframework
spring-web
${spring-framework.version}
org.springframework
spring-webmvc
${spring-framework.version}
cglib
cglib
3.1
org.springframework
spring-expression
${spring-framework.version}
org.springframework
spring-aop
${spring-framework.version}
org.springframework
spring-tx
${spring-framework.version}
org.springframework
spring-dao
2.0.3
com.fasterxml.jackson.core
jackson-core
2.2.3
com.fasterxml.jackson.core
jackson-databind
2.2.3
com.fasterxml.jackson.core
jackson-annotations
2.2.3
javax.servlet
javax.servlet-api
3.0.1
com.google.code.gson
gson
1.7.1
log4j
log4j
${log4j.version}
org.springframework.data
spring-data-commons
1.5.0.RELEASE
DashBoardService
src/main/resources
true
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.8
1.8
org.apache.maven.plugins
maven-war-plugin
2.6
false