Khi tôi chuyển ứng dụng thử nghiệm của mình sang ứng dụng (?) Hiệu quả và bắt đầu thử nghiệm trên Tomcat, dịch vụ REST dựa trên Spring 3.1 của tôi đã ngừng hoạt động. Mặc dù index.jsp mặc định được hiển thị, ứng dụng của tôi (http: // myhost: myport/test-webapp/myrestservice) không thể truy cập được và tôi nhận được Tài nguyên được yêu cầu (/ test-webapp/myrestservice) không có sẵn.Mùa xuân 3.1 REST với JSON: Không hoạt động
Dưới đây là những gì tôi đã làm:
Bộ điều khiển:
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(//...);
}
}
cấu hình văn Servlet:
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's 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("/");
}
}
Cấu hình lớp:
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
}
Và, web.xml có không nhiều nó:
<!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>
Không có gì khác ngoài dự án này. Tui bỏ lỡ điều gì vậy? Dự án cũ đã được thực hiện nhiều hơn một chút giống như đăng nhập các yêu cầu JSON đến vv (mà đã bị xóa bây giờ) đã được làm việc, nhưng tôi không có nó nữa: (Vì vậy, rất nhiều người mới blues. Xin hãy giúp tôi ở đây. Cảm ơn rất nhiều! !
cập nhật Issue được giải quyết. Đánh dấu vào câu trả lời
Xóa DOCTYPE, nó không hợp lệ - bạn đang nói ứng dụng đó là một ứng dụng phiên bản 2.3 và 3.0 của Servlet. – Pidster
Tôi nhận ra ngay sau khi tôi đăng câu trả lời :-) Cảm ơn bạn đã bình luận. – user1323865