Reputation: 716
For the below controller my "GET" method is working but my post method is not working . Th error is "POST method not supported"
My create method is working fine but update is not working.
and this is the curl command for my testing:
curl -H "Content-Type: application/json" -X POST -d '[{ id: 2, title: "My first note title update", note: "My First Note update", createTime: 1461737231000, lastUpdateTime: 1461737231000 } ]' -u "[email protected]":a localhost:8080/update
@RestController
public class GotPrintController {
@Autowired
private NotesRepository notesRepository;
@Autowired
private UserRepository userRepository;
@RequestMapping("/createnotes")
@Transactional
public Notes create() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
Notes notes = new Notes();
notes.setCreateTime(new Date());
notes.setLastUpdateTime(new Date());
notes.setNote("My First Note");
notes.setTitle("My first note title");
notesRepository.save(notes);
return notes;
}
@RequestMapping("/readnotes")
@Transactional
public List<Notes> read(){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
User users = userRepository.findByEmailId(auth.getName());
List<Notes> notes = (List<Notes>) users.getNotes();
return notes;
}
@RequestMapping(value="/delete/{id}/", method=RequestMethod.POST)
@Transactional
public String delete(@PathVariable Integer id){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
User users = userRepository.findByEmailId(auth.getName());
List<Notes> notes = (List<Notes>) users.getNotes();
for (Notes notes2 : notes) {
if(notes2.getId() == id){
notesRepository.delete(id);
return "success";
}
}
return "failure";
}
@RequestMapping(value="/update", method=RequestMethod.POST,headers = "Content-type: application/*")
@Transactional
public ResponseEntity<?> update(@RequestBody Notes note){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
User users = userRepository.findByEmailId(auth.getName());
List<Notes> notes = (List<Notes>) users.getNotes();
for (Notes notes2 : notes) {
if(note.getId() == notes2.getId()){
// note belongs to user update it
notesRepository.save(note);
return new ResponseEntity<>(note, HttpStatus.OK);
}
}
return new ResponseEntity<>("", HttpStatus.NO_CONTENT);
}
}
This is log when trying to hot POST method from curl
2016-04-27 13:15:36.927 INFO 14755 --- [ main] com.gotprint.GotprintApplication : Started GotprintApplication in 18.481 seconds (JVM running for 23.399) 2016-04-27 13:15:47.298 INFO 14755 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2016-04-27 13:15:47.298 INFO 14755 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2016-04-27 13:15:47.336 INFO 14755 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 38 ms 2016-04-27 13:15:47.433 WARN 14755 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : Request method 'POST' not supported
here is my entity class:
@Entity
public class Notes implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "title")
private String title;
@Column(name = "note")
private String note;
@Column(name = "createTime")
private Date createTime;
@Column(name = "lastUpdateTime")
private Date lastUpdateTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
Upvotes: 0
Views: 6321
Reputation: 3559
Just remove the
headers = "Content-type: application/*"
If you want to accept specific content type use the consumes property of the RequestMapping annotation.
Or of you want to use the headers property the syntax is:
headers = "Content-type=application/*"
Upvotes: 3