2017-04-21 143 views
0

我的應用程序出現問題。當我發送JSON數據時,返回錯誤415 - 服務器拒絕了此請求,因爲請求實體的格式不是所請求方法的請求資源所支持的格式。 我是新來的,我仍然試着理解這一點。將JSON數據發送到Spring MVC控制器 - 錯誤415

JSP頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
<script> 
jQuery(document).ready(function() { 
    $("#form-temp").submit(function(event) { 
     event.preventDefault(); 
     searchViaAjax(); 
    }); 
}); 

function searchViaAjax() { 

    var temp = {} 
    temp["temp"] = $("#temp").val(); 

    $.ajax({ 
     type : "POST", 
     contentType : "application/json", 
     url : "/update", 
     data : JSON.stringify(temp), 
     dataType : 'json', 
     cache: false, 
     timeout: 600000, 
     success : function(temp) { 
      console.log("SUCCESS: ", data); 
      //display(temp); 
     } 
    }); 

} 

/* function display(temp) { 
    var json = "<h4>Ajax Response</h4><pre>"+ JSON.stringify(data, null, 4) + "</pre>"; 
    $('#feedback').html(json); 
} */ 
</script> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Formularz wprowadzania temperatury</title> 
</head> 
<body> 

    <form id="form" method="post" action="update" id="form-temp"> 
     <div><label for="temp">Podaj temperaturę</label></div> 
     <div><input type="text" name="temp" id="temp"/></div> 
     <div><button id="sendBtn">Dodaj</button></div> 
    </form> 

    <form id="form" method="get" action="stats"> 
     <div><button id="sendBtn">Lista temperatur</button></div> 
    </form> 

</body> 
</html> 

控制器:

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

import pl.e.rekrutacja.domain.repository.TempRepository; 
import pl.e.rekrutacja.model.Temp; 

@RestController 
public class UpdateController { 

    @Autowired 
    private TempRepository tempRepository; 

// @ResponseBody 

    @RequestMapping(value = "/update", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") 
    public String startPage(@RequestBody Temp temp){ 
     tempRepository.addTemp(temp); 
     return "input_form_temp"; 
    } 

} 

模型類:

public class Temp { 

    private double value; 

    public double getValue() { 
     return value; 
    } 

    public void setValue(double value) { 
     this.value = value; 
    } 

    public Temp(double value) { 
     super(); 
     this.value = value; 
    } 
} 

應用程序上下文:

<?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" 
    xsi:schemaLocation="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 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <mvc:annotation-driven enable-matrix-variables="true"/> 
    <context:component-scan base-package="pl.e.rekrutacja" /> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 
     <property name="prefix"> 
      <value>/WEB-INF/jsp/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
    <bean id= "tempRepositoryImpl" class="pl.e.rekrutacja.domain.repository.impl.TempRepositoryImpl"/> 
</beans> 

web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
           http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 

     <filter> 
     <filter-name>encoding-filter</filter-name> 
     <filter-class> 
      org.springframework.web.filter.CharacterEncodingFilter 
     </filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
     <param-name>forceEncoding</param-name> 
     <param-value>true</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>encoding-filter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <servlet> 
     <servlet-name>DispatcherServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/webcontext/DispatcherServlet-context.xml 
      </param-value> 
     </init-param> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>DispatcherServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 
+0

嘗試添加默認構造函數Temp()以讓JSOn反序列化器實例化該類。 – StanislavL

+1

添加到您的控制器@ResponseBody並返回更新的Temp對象 – cralfaro

+0

@StanislavL您的解決方案無法正常工作。 – Sebastian

回答

0

您的控制器正在向請求的資源返回視圖名稱。相反,它必須以JSON格式返回Temp對象的狀態。

@RequestMapping(value = "/update", method = RequestMethod.POST) 
public @ResponseBody Temp tempStartPage(@PathVariable(@RequestBody Temp temp)) { 

return tempRepository.addTemp(temp); 
} 

@ResponseBody將處理consumes = "application/json", produces = "application/json"

+0

你的代碼是正確的?因爲我在粘貼時遇到Eclipse錯誤。 – Sebastian

+0

檢查新編輯的答案。 –

+0

好的,但我仍然有錯誤。 下劃線是溫度。 )‘結束MethodDeclaration \t - 語法錯誤,插入‘ - 「語法錯誤,插入這行 \t多個標記’類型VariableDeclaratorId’完成 \t FormalParameter \t - 令牌語法錯誤‘溫度’,(預期」 – Sebastian