1

I saw the relevant questions in stackoverflow but i didn't find a solution to my problem. This is my Initializer Class:

    public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
        public static HashMap<String, String> response_code = new HashMap<String, String>();


        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { MainConfiguration.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return null;
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            super.onStartup(servletContext);
            Security.addProvider(new BouncyCastleProvider());
            servletContext.addListener(new MainContextListener());
 }
}

This is the Controller:

@RestController 
@Component
public class MainController {
@RequestMapping(value = "/getAll", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Int addNumber (
            @RequestParam(value = "number", defaultValue = "0",
                    required = false) int number ) {
// code to set and return number
}
}

This is the Structure of webapp folder:

main/webapp/index.jsp

Css and Scripts folders are in webapp folder.

and I'm trying to run the project on intellij using tomcat web server. the problem is when I run the project, index.jsp opens in a browser but it gives 406 not acceptable uri error on getAll.

ShakibaZar
  • 727
  • 3
  • 9
  • 27

3 Answers3

0

You intercept all URLs (CSS as well)

protected String[] getServletMappings() {
    return new String[] { "/" };
}

You need tyo exclude resources from the intercepting. See e.g. Spring 4.x Java code-based configuration: Static resource files and Dispatcher servlet

Community
  • 1
  • 1
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I added this code to exclude the resources from being intercepted but nothing changed: "@Bean" public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/webapp/"); resolver.setSuffix(".jsp"); return resolver; } " @Override" public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/webapp/**") .addResourceLocations("/webapp/"); } – ShakibaZar Jan 24 '17 at 08:17
0

This should work

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

But make sure that you put the static contents under resources folder.

Please note that webapp is not a part of the Java web application folder structure and that's the reason why the code you put in your previous comment is not working

  • Now after adding ResourceHandlers another problem poped up! it is giving "406 not acceptable" on getAll. @Salvan Arekkattil – ShakibaZar Jan 24 '17 at 09:24
  • @user5621266 HTTP status 406 is specifically related to your controller method and the request posted. please go through the thread [Spring Controller post] (http://stackoverflow.com/questions/14006619/spring-requestparam-arguments-not-being-passed-in-post-method) to understand how to post a request to controller – Salvan Arekkattil Jan 24 '17 at 09:44
0

after trying lots of ways I discovered I had missed one dependency in my pom file, so response wasn't sent as json and 406 error occured, the missing dependency was jackson:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.3</version>
</dependency>
ShakibaZar
  • 727
  • 3
  • 9
  • 27