gpt4 book ai didi

spring - 已解决由 Handler 执行引起的异常 : org. springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form

转载 作者:行者123 更新时间:2023-12-04 01:57:01 24 4
gpt4 key购买 nike

我正在处理 Spring Boot MySQL 示例,链接如下: https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/ 。尝试访问时我在 url 下方

2018-04-14 22:29:54.987 WARN 9572 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

enter image description here

笔记 Controller

@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>

最佳答案

API 接受 application/json 格式的请求主体,而不是 application/x-www-form-urlencoded 格式。

检查下面的屏幕截图-

Sending application/json requests with Postman

您需要将请求主体作为 JSON 发送,并将 Content-Type 设置为 application/json,就像上面在 Postman 中一样。

关于spring - 已解决由 Handler 执行引起的异常 : org. springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49834022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com