我正在开发 https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/ 之后的 Spring Boot MySQL 示例。我在尝试访问
时收到了url2018-04-14 22:29:54.987 WARN 9572 --- [nio-8080-exec-3] .wsmsDefaultHandlerExceptionResolver:由Handler执行引起的已解决异常:org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'应用程序/ x-www-form-urlencoded;不支持charset = UTF-8
NoteController
@RestController
@RequestMapping("/api")
public class NoteController {
@Autowired
private NoteRepository noteRepository;
@GetMapping("/notes")
public List<Note> getAllNotes(){
return noteRepository.findAll();
}
@PostMapping("/notes")
public Note createNote(@Valid @RequestBody Note note){
return noteRepository.save(note);
}
@GetMapping("/notes/{id}")
public Note getNoteById(@PathVariable(value = "id") Long noteId){
return noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
}
@PutMapping("/notes/{id}")
public Note updateNote(@PathVariable(value = "id") Long noteId,
@Valid @RequestBody Note noteDetails){
Note note = noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
note.setTitle(noteDetails.getTitle());
note.setContent(noteDetails.getContent());
Note updatedNote = noteRepository.save(note);
return updatedNote;
}
@DeleteMapping("/notes/{id}")
public ResponseEntity<?> deleteNote(@PathVariable(value="id") Long noteId){
Note note = noteRepository.findById(noteId)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
noteRepository.delete(note);
return ResponseEntity.ok().build();
}
}
的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
答案 0 :(得分:2)
API接受application/json
格式的请求正文,而不是application/x-www-form-urlencoded
格式。
查看下面的屏幕截图 -
您需要将请求正文作为JSON发送,并在Postman中将Content-Type
设置为application/json
,如上所述。
答案 1 :(得分:1)
RequestBody
无法与application/x-www-form-urlencoded
一起使用,请改用@RequestParam
。
@PostMapping("/notes")
public Note createNote(@RequestParam Map<String, String> map) {
Note note = Note.create(map); // here Note#create method acts as static factory method which create note object from map
return noteRepository.save(note);
}