Leff
Leff

Reputation: 1350

Java spring - Posting a simple form not working

I am new to Java and Spring and I am trying to create a simple POST form. Template looks like this:

<form action="#" th:action="@{/notes}" method="POST">
  <input type="hidden" name="noteId" id="note-id">
  <div class="form-group">
    <label for="note-title" class="col-form-label">Title</label>
    <input type="text" name= "noteTitle" class="form-control" id="note-title" maxlength="20" required>
  </div>
  <div class="form-group">
    <label for="note-description" class="col-form-label">Description</label>
    <textarea class="form-control" name="noteDescription" id="note-description" rows="5" maxlength="1000" required></textarea>
  </div>
  <button id="noteSubmit" type="submit" class="d-none"></button>
</form>

And this is the controller for this POST endpoint:

@PostMapping("/notes")
    public String postNote(@ModelAttribute Note note, Authentication authentication, Model model) throws IOException {
        User user = this.userService.getUser(authentication.getName());
        Integer userid = user.getUserId();
        noteService.createNote(note, userid);
        model.addAttribute("success",true);
        model.addAttribute("message","New note added successfully!");
        return "result";
    }

The noteService createNote method looks like this:

public int createNote(Note note, Integer userid) {
    return noteMapper.insert(new Note(null, note.getNotetitle(), note.getNotedescription(), userid));
}

And the mapper looks like this:

@Insert("INSERT INTO CREDENTIALS (notetitle, notedescription, userid) VALUES(#{filename}, #{notetitle}, #{notedescription}, #{userid})")
@Options(useGeneratedKeys = true, keyProperty = "noteid")
int insert(Note note);

This is the model:

public class Note {
    private Integer noteid;
    private String notetitle;
    private String notedescription;
    private Integer userid;

    public Note(Integer noteid, String notetitle, String notedescription, Integer userid) {
        this.noteid = noteid;
        this.notetitle = notetitle;
        this.notedescription = notedescription;
        this.userid = userid;
    }

    public Integer getNoteid() {
        return noteid;
    }

    public void setNoteid(Integer fileId) {
        this.noteid = noteid;
    }

    public String getNotetitle() {
        return notetitle;
    }

    public void setNotetitle(String filename) {
        this.notetitle = notetitle;
    }

    public String getNotedescription() {
        return notedescription;
    }

    public void setNotedescription(String notedescription) {
        this.notedescription = notedescription;
    }

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) { this.userid = userid; }
}

And this is the sql schema:

CREATE TABLE IF NOT EXISTS NOTES (
    noteid INT PRIMARY KEY auto_increment,
    notetitle VARCHAR(20),
    notedescription VARCHAR (1000),
    userid INT,
    foreign key (userid) references USERS(userid)
);

When I try to post a note I get an error:

There was an unexpected error (type=Not Found, status=404). No message available

When I run it in debug mode it doesn't even get to the controller. What am I doing wrong here? This is the repo.

Upvotes: 1

Views: 143

Answers (3)

Rabhi salim
Rabhi salim

Reputation: 506

Then the API wasn't even exposed.

If you run the project you should get the list of exposed APIs in the log. Such as

DEBUG 9736 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 19 mappings in 'requestMappingHandlerMapping'

If you use Spring Boot Actuator on intellij: you can check list of API in Endpoints tab, it can be really helpful: enter image description here

Upvotes: 1

Gewure
Gewure

Reputation: 1268

As @Leff figured out himself by now, he was simply missing

@RestController
public class NoteController {

...above his NoteController Class.

@RestController tells Spring that the underlying class is a Rest-Endpoint Bean.

@Restcontroller inherits from @Controller, see Spring Boot Doc:

The first annotation on our Example class is @RestController. This is known as a stereotype annotation. It provides hints for people reading the code and for Spring that the class plays a specific role. In this case, our class is a web @Controller, so Spring considers it when handling incoming web requests.

The @RequestMapping annotation provides “routing” information. It tells Spring that any HTTP request with the / path should be mapped to the home method. The @RestController annotation tells Spring to render the resulting string directly back to the caller.

Upvotes: 1

Rabhi salim
Rabhi salim

Reputation: 506

Error 404 usually means you have wrong url. (https://httpstatuses.com/404)

That explains why " it doesn't even get to the controller."

Double check that you are pointing to the right API url

Upvotes: 0

Related Questions