我遇到了 Spring REST Web 服务的问题。当我尝试发布 JSON 时,我总是收到 404 错误。获取请求工作得很好,但不能发布。在整个互联网上搜索但找不到类似的问题。也许你们可以帮助我。这是我的代码:
Controller :
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);
}
型号:
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;
}
POM.xml:
<?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>
我发布的 Json:
{
"sensorId" : 1,
"temperture" : 20,
"date" : "2016-10-20 22:10:10"
}
编辑:
我得到的错误:
{
"timestamp": 1477601444227,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/rest/temperature/"
}
提前致谢!
编辑 2:
我刚刚使用相同的 Controller 创建了新项目,一切正常。正如我发现问题出在 spring 安全性上,出于某种原因,即使我允许所有请求,Post 和 Put 方法也不起作用。所以这是我的问题。
请您参考如下方法:
使用 @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
在客户端。