Ich versuche, Restful Web Services zu erstellen. Mein Maven-Projektname ist Rest und ich folge Spring's Building a RESTful Web Service, um das zu tun, aber ich möchte Spring Boot nicht verwenden, sondern nur einen Krieg erstellen und es auf Tomcat in meinem eclise/STS hosten. Hier sind meine web.xml und XX-servlet.xml Dateien:Problem beim Ausführen von Spring Restful Web Services ohne Spring Boot
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
rest-servlet.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.rest" />
<mvc:annotation-driven/>
</beans>
GreetingController.java
@RestController
public class GreetingController2 {
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format("Hello %s", name));
}
}
Wenn ich dies auf Tomcat ausführen die URL: http://localhost:8080/rest gibt 404 und ich sehe diese Nachricht in Tomcat-Konsole Aug 16, 2017 3:59:28 PM org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/rest/] in DispatcherServlet with name 'rest'
, während ich die Zuordnung habe.
Und wenn ich http://localhost:8080/rest/greeting hit bekomme ich http 406 mit Nachricht The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
, während nach dem Tutorial sollte es in JSON konvertiert werden, die im Browser gerendert werden kann.
Ich verbrachte viel Zeit damit, herauszufinden, wie man verschiedene Beiträge auf SO findet, um herauszufinden, was falsch ist, aber nicht konnte.
Akshay Srivastava Antwort hat mir geholfen, das Problem finden sollte. Zusätzlich zu dem, was er erwähnte, hatte ich keine Jackson JSON-Abhängigkeiten, weshalb es mein Objekt nicht in JSON konvertieren konnte. Als ich diese zu meiner pom.xml hinzugefügt habe, konnte ich http: // localhost: 8080/rest/gruß arbeiten sehen. – Learner