Spring 3.1 REST with JSON: Doesn't work

As soon as I moved the test application to a productive (?) Application and started testing on Tomcat, my REST services based on Spring 3.1 stopped working. Although the index.jsp index is displayed by default, my application (http: // myhost: myport / test-webapp / myrestservice) is not available and I get the Requested resource (/ test-webapp / myrestservice) is not available.

Here is what I did:

Controller:

package com.test.webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.webap.entity.Account;

@Controller
@RequestMapping("/myrestservice")
public class AccountController{

@RequestMapping(method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Account getEntity() {
            // ... <simplified>
    return new Account(//...);
}
}

Servlet Manager Configuration:

package com.test.webapp.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;   
import com.test.webapp.config.AppConfig;

public class AppInit implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

    public void onStartup(ServletContext container) throws ServletException {
        // Create the dispatcher servlet Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(AppConfig.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(dispatcherContext));

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet(
                DISPATCHER_SERVLET_NAME, new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }
}

Configuration Class:

package com.test.webapp.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan( basePackages = {"com.test.webapp.controller"})
public class AppConfig{
    // Nothing here. Once I start using the beans, I will put them here
}

And there is not much in web.xml:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Test Web App</display-name>
</web-app>

There is nothing special about my project. Am I missing something? An old project that was a bit like registering incoming JSON requests, etc. (Which has now gone), it works, but I no longer have it :( So, a lot of New Year's blues. Please help me here. Thank you very much!

.

+5
4

. , Servlet 3.0, - Tomcat 7 . web.xml, :

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    metadata-complete="false">

    <display-name>Test Web App</display-name>
</web-app>

-, Tomcat ServletContainerInitializer, web.xml metadata-complete = "false"

+2

Tomcat ? , Tomcat 7, Servlet 3.0, WebApplicationInitializer.

+2

Tomcat Manager , ? . , .

0

Since you're new to Spring, I would recommend you take a look at my previous answer:

The requested resource (/) is unavailable

0
source

All Articles