Announcement Announcement Module
Collapse
No announcement yet.
Restful JSON Requests Returns 404 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Restful JSON Requests Returns 404

    I'm currently trying to get a simple Hello World type example working and I'm receiving 404 - Not Found when invoking my web service. I apologize if this is a newbie type question, but I'm beyond frustrated. I believe it's a configuration issue because I'm not even reaching the controller. Any help would be greatly appreciated or even help in enabling debugging log4j throws AccessControlException which seems to related to Tomcat 6.

    My call is http://localhost/rest/service/emps

    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      id="WebApp_ID" version="2.5">
      
      <display-name>Redis</display-name>
      
      <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>Redis</param-value>
      </context-param>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
      
      <!-- The context params that read by ContextLoaderListener  -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          /WEB-INF/rest-context.xml
        </param-value>
      </context-param>
      
      <!-- This listener will load other application context file in addition to springweb-servlet.xml -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>debug</param-name>
          <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/service/*</url-pattern>
      </servlet-mapping>
    </web-app>
    rest-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
      <context:component-scan base-package="com.redis.ws.controller" />
      <!-- To enable @RequestMapping process on type level and method level -->
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
          <list>
            <ref bean="marshallingConverter" />
            <ref bean="jsonConverter" />
          </list>
        </property>
      </bean>
    
      <bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
          <constructor-arg ref="jaxbMarshaller" />
          <property name="supportedMediaTypes" value="application/xml"/>
      </bean>
    
      <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json" />
      </bean>
    
      <!-- Client -->
      <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="messageConverters">
          <list>
            <ref bean="marshallingConverter" />
            <ref bean="jsonConverter" />
          </list>
        </property>
      </bean>
      
      <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
          <list>
            <value>com.redis.ws.bean.Employee</value>
            <value>com.redis.ws.bean.EmployeeList</value>
          </list>
        </property>
      </bean>
    
      <bean id="employees" class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg ref="jaxbMarshaller" />
      </bean>
    
      <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml"/>
                <entry key="html" value="text/html"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
                <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
              <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
              <property name="prefix" value="/WEB-INF/jsp/"/>
              <property name="suffix" value=".jsp"/>
            </bean>
            </list>
        </property>
      </bean>
      
      <!--bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" /-->
    
      <bean id="employeeController" class="com.redis.ws.controller.EmployeeController">
        <property name="employeeDS" ref="employeeDS" />
        <property name="jaxb2Mashaller" ref="jaxbMarshaller" />
      </bean>
    </beans>
    rest-context.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
      <bean id="employeeDS" class="com.redis.ws.ds.EmployeeDS" />
    </beans>
    Code:
    package com.redis.ws.controller;
    
    import com.redis.ws.bean.Employee;
    import com.redis.ws.bean.EmployeeList;
    import com.redis.ws.ds.EmployeeDS;
    
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    @Controller
    public class EmployeeController {
    
        private EmployeeDS employeeDS;
        
        public void setEmployeeDS(EmployeeDS ds) {
            this.employeeDS = ds;
        }
        
        ////////////////////////// @ResponseBody ////////////////////////
        
        @RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/xml, application/json")
        public @ResponseBody Employee getEmp(@PathVariable String id) {
            Employee e = employeeDS.get(Long.parseLong(id));
            return e;
        }
        
        @RequestMapping(method=RequestMethod.GET, value="/emps", headers="Accept=application/xml, application/json")
        public @ResponseBody EmployeeList getAllEmp() {
            List<Employee> employees = employeeDS.getAll();
            EmployeeList list = new EmployeeList(employees);
            return list;
        }
    }

  • #2
    Which spring version? Post the logging.

    Also crank up the log level to DEBUG and see what happens to your request.

    Comment


    • #3
      Spring 3.0.5

      I cannot get logging enabled (unless I'm really really dense) due to an aforementioned issue with Tomcat 6. Unless there is another way to accomplish logging without doing the following:

      Code:
      <?xml version="1.0" encoding="UTF-8" ?>  
      <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
        
      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  
        
        <appender name="console" class="org.apache.log4j.ConsoleAppender">  
          <param name="Target" value="System.out"/>  
          <layout class="org.apache.log4j.PatternLayout">  
            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n"/>  
          </layout>  
        </appender>  
        
        <root>  
          <priority value ="error" />  
          <appender-ref ref="console" />  
        </root>  
        
        <category name="org.springframework" additivity="false">  
          <priority value="debug" />  
          <appender-ref ref="console" />  
        </category>  
        
      </log4j:configuration>
      And adding the following lines (in this order) to the very top of my web.xml:
      Code:
      <context-param>  
        <param-name>log4jConfigLocation</param-name>  
        <param-value>/WEB-INF/log4j.xml</param-value>  
      </context-param>  
      <listener>  
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
      </listener>
      Any help either getting debugging up and running or if somebody sees something obviously wrong in my config is super appreciated.

      Comment


      • #4
        Are you using port 80 to develop? By default Tomcat responds on 8080.
        I can't see anything wrong in your conf files.

        Stefano

        Comment

        Working...
        X