0

Пытаюсь добавить новую запись в БД, но попытки заканчиваются ошибкой

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

Собственно класс контроллера:

@Controller
public class NoteController {
    @Autowired
    private NoteService service;

    @GetMapping("/notes")
    public String notes(Model model) {
        List<Note> listOfNotes = service.findAll();
        Note newNote = new Note();
        model.addAttribute("notes", listOfNotes);
        model.addAttribute("newNote", newNote);
        return "index";
    }

    @PostMapping("/create")
    public String createNote(@RequestBody Note note) {
        service.createNote(note);
        return "redirect:/notes";
    }

    @GetMapping("/{id}")
    public String deleteNote(@PathVariable String id) {
        Note note = service.getById(Integer.parseInt(id));
        service.deleteNote(note);
        return "redirect:/notes";
    }
}

HTML страница c формой добавления:

<body>
	<h1>Заметки</h1>
	<!-- <a href="addPerson">Add Person</a> -->
	<br />
	<br />

	<ul>
		<li th:each="note : ${notes}">
			<div>
				<h2 th:utext="${note.name}">...</h2>
				<p th:utext="${note.text}">...</p>
				<a th:href="@{${note.id}}"> 
					<span>Удалить</span>
				</a>
			</div>
		</li>
	</ul>

	<form th:action="@{/create}" th:object="${newNote}" method="POST">
		Название: <input type="text" th:field="*{name}" size="35" /> <br />
		Текст: <textarea rows="3" cols="30" name="text"></textarea> <br />
		<input type="submit" value="Добавить" />
	</form>
</body>

1 ответ 1

0

Spring не понимает application/x-www-form-urlencoded пока ему не поможешь, например, так:

@RequestMapping("/create", method = RequestMethod.POST, 
    consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
    produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}))
public String createNote(@RequestBody Note note) {
    service.createNote(note);
    return "redirect:/notes";
}
1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.