我的申请有问题。当我发送JSON数据时,我得到返回错误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 :(得分: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"
。