已解决处理程序执行导致的异常:org.springframework.web.HttpMediaTypeNotSupportedException:Content type 'application/x-www-form

voase2hg  于 2023-06-05  发布在  Spring
关注(0)|答案(3)|浏览(468)

我正在做Sping Boot MySQL示例如下链接:https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/。我得到下面的网址时,试图访问
2018-04-14 22:29:54.987 WARN 9572 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver:已解决处理程序执行导致的异常:org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application/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>
iezvtpos

iezvtpos1#

API接受application/json的请求体,而不是application/x-www-form-urlencoded
查看下面的截图-

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

ff29svar

ff29svar2#

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);
}
8wtpewkr

8wtpewkr3#

2023-06-05T16:51:46.608+05:30 WARN 16288 --- [nio-8085-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver:已解决[org.springframework.http.converter.HttpMessageNotReadableException:JSON解析错误:无法构造com.example.RoleBaed_Authorization_and_RestTemplate.DtoClass.LoginDto的示例(尽管至少存在一个Creator):没有要从String值反序列化的String参数构造函数/工厂方法('com.example.RestTemplatePractice.Entity.ApiRes1')]

相关问题