运行 Spring Restful Web 服务中的问题 Spring 引导
Issue in running Spring Restful web services without Spring Boot
我正在尝试构建 Restful 网络服务。我的 Maven 项目名称是 rest 我正在按照 Spring's Building a RESTful Web Service 来做,但我不想使用 Spring 启动但只需创建一个 war 并将其托管在我的 eclise/STS 中的 Tomcat 上。这是我的 web.xml 和 XX-servlet.xml 文件:
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>
休息-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));
}
}
当我在 Tomcat 上 运行 URL: http://localhost:8080/rest 给出 404 并且我在 tomcat 控制台 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'
中看到这条消息而我确实有映射。
当我点击 http://localhost:8080/rest/greeting 时,我收到带有消息 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
的 http 406,而根据教程,它应该被转换为 JSON,可以在浏览器中呈现。
我花了很多时间试图找出并查看 SO 上的各种帖子以找出问题所在,但无法找到。
您没有像
这样的默认映射
@RequestMapping("/")
根据你上面的代码,你应该有以下 url 工作正常
http://localhost:8080/rest/greeting
我正在尝试构建 Restful 网络服务。我的 Maven 项目名称是 rest 我正在按照 Spring's Building a RESTful Web Service 来做,但我不想使用 Spring 启动但只需创建一个 war 并将其托管在我的 eclise/STS 中的 Tomcat 上。这是我的 web.xml 和 XX-servlet.xml 文件:
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>
休息-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));
}
}
当我在 Tomcat 上 运行 URL: http://localhost:8080/rest 给出 404 并且我在 tomcat 控制台 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'
中看到这条消息而我确实有映射。
当我点击 http://localhost:8080/rest/greeting 时,我收到带有消息 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
的 http 406,而根据教程,它应该被转换为 JSON,可以在浏览器中呈现。
我花了很多时间试图找出并查看 SO 上的各种帖子以找出问题所在,但无法找到。
您没有像
这样的默认映射
@RequestMapping("/")
根据你上面的代码,你应该有以下 url 工作正常
http://localhost:8080/rest/greeting