我遇到了Spring REST Web服务的问题。当我尝试发布JSON时,我总是得到404错误。获取请求工作正常,但不发布。在互联网上搜索但找不到类似的问题。也许你们可以帮助我。这是我的代码:
控制器:
from itertools import product
from random import shuffle
all_cards = list(product(PIPS, SUITES))
shuffle(all_cards)
just_10_cards = all_cards[:10]
for p, suite in just_10_cards:
print(p, suite, end='')
print()
}
型号:
package com.java.rest;
import com.java.dto.IncomingTemperature;
import com.java.dto.SensorsTemperature;
import com.java.service.GetSensorsTemperature;
import com.java.service.TemperatureService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/rest")
public class TemperatureRestController {
final static Logger logger = LoggerFactory.getLogger(TemperatureRestController.class);
@Autowired
TemperatureService temperatureService;
@Autowired
GetSensorsTemperature sensorService;
@RequestMapping(value = "/temperature", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public ResponseEntity<?> addTemperature(@RequestBody IncomingTemperature temperature){
logger.info("Temperature: {} recieved", temperature);
temperatureService.addTemperature(temperature);
return new ResponseEntity<HttpStatus>(HttpStatus.CREATED);
}
@RequestMapping(value = "/temperature/{sensorId}", method = RequestMethod.GET, produces = "application/json")
public SensorsTemperature getTemperature(@PathVariable("sensorId") Long sensorId){
logger.info("Getting temperatures for sensor{}", sensorId);
return sensorService.getSensorsTemperature(sensorId);
}
的pom.xml:
package com.java.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.util.Date;
public class IncomingTemperature implements Serializable{
private long sensorId;
private double temperature;
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
private Date date;
public long getSensorId() {
return sensorId;
}
public void setSensorId(long sensorId) {
this.sensorId = sensorId;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
我发布的Json:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java</groupId>
<artifactId>knowyourheat</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>KnowYourHeat</name>
<description>Application for temperature monitoring</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编辑:
我得到的错误:
{
"sensorId" : 1,
"temperture" : 20,
"date" : "2016-10-20 22:10:10"
}
提前谢谢!
编辑2:
我刚刚使用相同的控制器创建了新项目,一切正常。正如我发现问题是弹簧安全性,出于某种原因,即使我允许所有请求,Post和Put方法也不起作用。这就是我的问题。
答案 0 :(得分:0)
尝试添加&#34;消耗=&#34; application / json&#34;在你的@RequestMapping中:
@RequestMapping(value = "/temperature", method = RequestMethod.POST, produces = "application/json", consumes="application/json")
答案 1 :(得分:0)
尝试添加&#34; headers =&#34; Accept = application / json&#34;&#34;在你的RequestMapping中这样:
@RequestMapping(value = "/temperature", method = RequestMethod.POST, headers = "Accept=application/json")
答案 2 :(得分:0)
使用@RestController
时,您在方法中不需要@ResponseBody
。您获得404
因为该方法无法接受任何类型的正文内容。当该方法仅从HTTP请求接收内容但未发回任何正文内容时,您只需要consumes
。 ResponseEntity<xxx>
定义除了HttpStatus之外该方法将返回的正文内容。如果它不返回正文内容而只返回HttpStatus,则必须使用ResponseEntity<Void>
。请尝试以下
@RequestMapping(value = "/temperature", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> addTemperature(@RequestBody IncomingTemperature temperature){
logger.info("Temperature: {} recieved", temperature);
temperatureService.addTemperature(temperature);
return new ResponseEntity<Void>(HttpStatus.CREATED);
}
现在你应该在客户端获得HttpStatus 201 Created
。