简体   繁体   中英

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. I am trying to send json request to localhost/test and wish I could get any response, but invain.

This is my web.xml file

<!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

<?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:

@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?!". Another solution is use @ResponseBody to JSON - link .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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