2016-12-15 2 views
0

스프링 4 기능을 사용하기 위해 애플리케이션을 마이그레이션하려고합니다. 그래서 XML 설정 (web.xml, applicationContext.xml, dispatcher-servlet.xml)을 제거하고 을 Java annotations으로 대체하려고합니다. 나는이 문제에 많은 시간을 보냈고, 나는 또한 stackoverflow에서 해결책을 찾을 수 없었다. [] [행 번호 - - 1136] 이름의 DispatcherServlet에 URI [/ WP/xhStatus]로 HTTP 요청을 찾을 수 없습니다 매핑 '디스이름이 'dispatcher'인 DispatcherServlet에 URI가있는 HTTP 요청에 대한 매핑이 없습니다.

org.springframework.web.servlet.PageNotFound : 나는 아래의 오류를 얻고있다 '. "http://localhost:8080/WP/xhStatus"에 대한 POST 요청으로 404 (null)가 발생했습니다. 오류 처리기를 호출하는 것은 org.springframework.web.client.HttpClientErrorException :이 오류 메시지에서 (404) 널 (null)

, 내가 볼려고 경우에서 봄 API 패키지 org.springframework.web.servlet에서 사용할 수 PageNotFound URL하지만 패키지에 이러한 클래스 또는 사용 가능한 인터페이스 아래 : 난 그냥 추가 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/package-summary.html

이 아래 클래스 WebAppInitializer.java & WebConfig.java 일부 코드가 web.xml디스패처 - servlet.xml에서 제거되었습니다.

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
,174,515 :

WebAppInitializer.java

package com.mycompany.wp.web.config; 

public class WebAppInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) throws ServletException { 
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); 
    applicationContext.setConfigLocations("com.mycompany.wp.web.config.WebConfig"); 
    servletContext.addListener(new ContextLoaderListener(applicationContext)); 

    servletContext.setInitParameter("contextConfigLocation", "/WEB-INF/applicationContext.xml"); 

    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext)); 
    dispatcherServlet.setLoadOnStartup(1); 
    dispatcherServlet.addMapping("/"); 
} 

} 

WebConfig.java

package com.mycompany.wp.web.config; 

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages="com.mycompany.wp") 
public class WebConfig extends WebMvcConfigurerAdapter{ 

@Bean 
public InternalResourceViewResolver viewResolver() { 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setViewClass(JstlView.class); 
    viewResolver.setPrefix("/views/"); 
    viewResolver.setSuffix(".jsp"); 
    return viewResolver; 
} 

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

난은 의 web.xml에서이 부분을 삭제 여전히

<mvc:annotation-driven /> 

내가 :

<mvc:resources mapping="/resources/**" location="/resources/" /> 

<context:component-scan base-package="com.mycompany.wp"/> 

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

이제 디스패처-servlet.xml에 하나 개의 항목이 :

는 또한 내가 디스패처-servlet.xml에의 아래 부분은 제거 그들은 여전히 ​​다른 정보가 있기 때문에 모든 XML 파일 (web.xml, dispatcher-servlet.xml & applicationContext.xml)을 내 응용 프로그램에 포함해야합니다. 그러나 applicationContext.xml에는 변경 사항이 없습니다.

/xhStatus과 같은 요청 매핑을 사용하여 REST 서비스 (스프링 MVC 컨트롤러)를 호출하려고하는데 아무 것도 변경하지 않았다. 이전에 위에서 언급 한 URL에서 내 REST 서비스에 연결할 수 있었지만 지금은 도달 할 수 없습니다.

Java 구성으로 이동하기 전에 내 응용 프로그램은 XML 구성에서 잘 작동합니다. DispatcherServlet이 Spring Controller를 찾지 못했을 수 있습니다.

누군가 나를 도울 수 있다면 좋을 것입니다. 고맙습니다.

답변

0

이 문제가 해결되었습니다.

applicationContext.register(com.mycompany.wp.web.config.WebConfig.class); 

그것은 잘 작동 :

applicationContext.setConfigLocations("com.mycompany.wp.web.config.WebConfig"); 

지금 내가 가진 그 라인을 교체 : 내가 사용 WebAppInitializer.java

에서 실수를했다.