2017-04-21 113 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

相关问题