我正在尝试访问REST服务:
http://localhost:8080/events
我使用这个adres获得404,我不知道为什么。我试图用GET返回JSON。 这是控制器:
package com.app.eventapp;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import lukpra.jba.entity.Event;
import lukpra.jba.service.EventService;
@Controller
public class EventController {
@Autowired
private EventService eventService;
@RequestMapping(value = "/events" , method = RequestMethod.GET)
public String events(Model model){
model.addAttribute("events", eventService.findAll());
return "events";
}
public @ResponseBody ResponseEntity <List<Event>> getEvents(){
List<Event> events = eventService.findAll();
return new ResponseEntity<List<Event>>( events, HttpStatus.OK);
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResponseEntity<List<Event>> getRestEvents(){
List<Event> events = eventService.findAll();
return new ResponseEntity<List<Event>>( events, HttpStatus.OK);
}
}
我的web.xml:
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>event_app</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>*.html</url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.json</url-pattern>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
和调度员:
<context:component-scan base-package="com.app.eventapp"></context:component-scan>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/general.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
</beans>
问题必须在映射中的某处。我可以在没有任何问题的情况下显示来自控制器数据的事件页面,所以这肯定是有效的。
我在jetty控制台中没有任何错误日志。它可能是url路径,但添加:
<url-pattern>/</url-pattern>
打破了应用。
更新 我的调度员接受格式的链接,例如:&#34; * .json&#34;。所以我尝试将此扩展添加到映射:
@RequestMapping(value="/test.json", produces = {
现在我回忆HTTP ERROR 406 Problem accessing /test.json. Reason: Not Acceptable
还有一些hibernate的日志在调用时会选择select,这是个好兆头。
答案 0 :(得分:0)
我对Jetty不熟悉,但我认为它就像tomcat一样。在tomcat中,您需要将上下文路径重置为&#34; /&#34;这样您就可以忽略URL中的项目名称。您可以检查此website以重置上下文路径。希望这会有所帮助。