2015-02-10 21 views
0

我試了一會,這個東西怎麼沒有得到迴應?它始終無法找到URL映射/ 404未找到。 我想發送json請求到本地主機/測試,並希望我能得到任何迴應,但invain。今年春季發送json得到了404

這是我的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> 

這是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> 

這是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?!"; 
    }  
} 
+0

請分享你調用的URL。 – Mithun 2015-02-10 08:50:25

回答

0

可能,您應該從getBid(...)邏輯名稱(「test」)返回 - 不返回s像這樣的充足的字符串 - >「是工作?!」。另一個解決方案是使用JSON的@ResponseBody - link

0

春天不會明白,TestController是控制器,除非你有在你的配置<mvc:annotation-driven/>,否則你需要@ResponseBody跳過視圖解析器

+0

我添加了它,現在我只能通過POST方法獲得RequestBody並將正文字符串傳遞給/ test,但是當我使用GET方法時,正文是空的,是否有方法在/ test中使用GET方法傳遞正文字符串?哪種方法可以將json字符串返回給請求? – 2015-02-11 08:47:07