0

Hi i have a trouble in my Java spring application, when i call my rest controller it execute 2 times.

This is my Controller

@Controller
public class CrashReportController extends AbstractBaseController {

private final Logger logger = LoggerFactory.getLogger(CrashReportController.class);

private CrashReportRequest crashReportRequest;


@Override
protected Logger getLogger() {
    return logger;
}

@RequestMapping(value = "/req/{token}", method = RequestMethod.POST, consumes = "application/json", produces = "application/pdf")
public @ResponseBody ResponseEntity<byte[]> req(@RequestBody CrashReportRequest request, @PathVariable ("token") String token)
        throws IOException {...}

}

This is my web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>    

<display-name>rmp</display-name>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>resources/app-config.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>my-dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>resources/web-config.xml</param-value>
    </init-param>      
</servlet>

<servlet-mapping>
    <servlet-name>my-dispatcher-servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

App-config.xml

<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"
   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 ">


<context:component-scan base-package="rmp.rest.controller">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

Web-config

<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/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd ">

<context:component-scan base-package="rmp.rest.controller">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
</bean>

Why i have the double initilize (i suppose this is the problem)? I'm gettin crazy to solve this, please HELP!

1 Answer 1

0

Your configuration makes the package "rmp.rest.controller" to be scanned twice (and therefore all the beans on it to be initialized twice).

This is caused by the following elements. In App-config.xml:

<context:component-scan base-package="rmp.rest.controller">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

And in Web-config.xml:

<context:component-scan base-package="rmp.rest.controller">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

Removing the previous element from App-config.xml seems the path to follow.

6
  • Thank you Andres, so what i have to change? App-config? Web-config?
    – Michele
    Mar 27, 2017 at 9:27
  • I totally removed the context:component-scan tag from App-config, but it still executes 2 times. I'm very disappointed, tried everything ...
    – Michele
    Mar 27, 2017 at 9:38
  • @Michele could do you add the JSP code or html where you call to req/{token}?
    – cralfaro
    Mar 27, 2017 at 9:39
  • Is a rest call, i use postman from chrome plugins. I run my locale tomcat and invoke this url: localhost:8080/rmp/req/token1
    – Michele
    Mar 27, 2017 at 9:42
  • @Michele if you add a breakpoint in the POST method of your controller, and executes the first request, postman its waiting for a result or its showing the result from your controller?
    – cralfaro
    Mar 27, 2017 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.