2012-03-17 3 views
0

스프링 설정에 대한 경험이 거의 없으므로 힘들어하고 있습니다. 내가 뭘 하려는지, 주석을 사용하여 성공적으로 컨트롤러에 URL을 매핑합니다. 또한 HttpRequest 및 선택적으로 HttpResponse에 대한 액세스 권한을 갖고 싶습니다. 왜냐하면 잭슨을 직접 사용하여 바이트 스트림을 쓰고 파싱하는 것이기 때문입니다. 이제 JSON이 내장 된 Jackson을 사용하여 Spring에 JSON 뷰가 있음을 알았지 만 매핑을 제대로 구성 할 수없는 것처럼 이제는 적절한 발판을 갖기를 원합니다.스프링 컨트롤러 맵핑 설정

web.xml을

<display-name>Kerris 2</display-name> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/*-config.xml 
    </param-value> 
</context-param> 

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

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

앱 confix.xml

<context:annotation-config /> 
<context:component-scan base-package="servlet" /> 

<bean 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
<bean 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

DayController

package servlet; 

@Controller 
@RequestMapping("/days/*") 
public class DayController { 

    private DayDAO dayDao; 

    @RequestMapping(method = RequestMethod.GET) 
    public @ResponseBody void test(HttpResponse response){ 
     System.out.println("Days GET"); 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    public @ResponseBody void test2(HttpRequest request, HttpResponse response){ 
     System.out.println("Days POST"); 
    } 

    public void setDaydao(DayDAO dayDao) { 
     this.dayDao = dayDao; 
     System.out.println("Days Dao assigned"); 
    } 

} 

내가 볼 내 서버 로그는 내가 이 컨텍스트에서 응용 프로그램을 테스트 할 때 또한/ 내가 표준 안녕하세요 참조가

INFO: Mapped URL path [/days/*] onto handler 'dayController' 
INFO: Mapped URL path [/days/*.*] onto handler 'dayController' 
INFO: Mapped URL path [/days/*/] onto handler 'dayController' 

에 다음 줄을 볼 수 있습니다! 페이지. contextroot/days/을 시도하면 404가됩니다. contextroot/days/test도 404입니다. 내가 잘못하고있는 부분을 지적 해줄 수 있습니까?

답변

0

내가 잘못한 것을 발견했습니다. n00b처럼 컨트롤러에 요청을 전달하는 DispatcherServlet을 구체적으로 정의해야한다는 것을 알지 못했습니다.

의 Web.xml

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring/*-config.xml 
    </param-value> 
</context-param> 

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

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

<servlet> 
    <servlet-name>api</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>api</servlet-name> 
    <url-pattern>/api/*</url-pattern> 
</servlet-mapping> 

API-servlet.xml에

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 



<context:annotation-config /> 
<context:component-scan base-package="servlet" /> 

<bean 
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
<bean 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

</beans> 

내가 {컨텍스트 루트를} 전화를 시도/API는/일/요청은 컨트롤러에 도달합니다.