简体   繁体   English

今年春天发送json得到404

[英]This spring send json got 404

I've tried for awhile, how come this thing does not get a response? 我已经尝试了一段时间,为什么这个东西没有得到回应? It always couldn't find the url mapping/404 not found. 它总是找不到网址映射/ 404。 I am trying to send json request to localhost/test and wish I could get any response, but invain. 我正在尝试将json请求发送到localhost / test,希望我能得到任何响应,但没有用。

This is my web.xml file 这是我的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>
      <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>/*</url-pattern>
      </servlet-mapping>
 </web-app>

This is the dispather-servlet.xml file 这是dispather-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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring- beans-3.0.xsd">

    <bean name="test" class="com.my.controller.TestController"></bean>     

</beans>

This is the TestController.java: 这是TestController.java:

@Controller
@RequestMapping("/")
public class TestController {
@RequestMapping(value = {"/test"}, headers =   "Accept=application/json")
    public String getBid(HttpServletRequest request, HttpServletResponse  response) throws Exception {
        System.out.println("test...");
        return "is itworking?!";
    }     
}

Probably, You should return from getBid(...) logic name of view ("test") - don't return sample string like this -> "is itworking?!". 可能,您应该从getBid(...)逻辑视图名称(“测试”)返回-不要返回像这样的示例字符串->“正在工作吗?!”。 Another solution is use @ResponseBody to JSON - link . 另一个解决方案是使用@ResponseBody到JSON 链接

除非您在配置中包含<mvc:annotation-driven/> ,否则Spring将不会理解TestController是一个控制器,否则,您需要@ResponseBody来跳过View解析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM