0

I am trying to update data in my database. I am using jQuery/AJAX on frontend and REST/MyBatis/MySQL on backend. Unfortunately REST controller returns 404 status code. Please look at my REST controller code:

@RestController @RequestMapping("/documents") public class DocumentResources { private DocumentsMapper mapper; public DocumentResources(DocumentsMapper mapper) { this.mapper = mapper; } @PostMapping("/updateDocument") public List<Documents> updateDocument (@RequestBody Documents document) { mapper.updateDocument(document); return mapper.getAllDocuments(); } } 

Here is my DocumentsMapper class code:

@Mapper public interface DocumentsMapper { @Select("select * from documents") List<Documents> getAllDocuments(); @Update("UPDATE documents SET title = #{title}, author = #{author}, src = #{src} WHERE id =#{id}") void updateDocument(Documents document); } 

And here is my AJAX method:

$( "#target" ).submit(function( event ) { event.preventDefault(); var formData = { id : $("#target #id").val(), title : $("#target #title").val(), author : $("#target #author").val(), src: $("#target #src").val(), createTime : $("#target #createTime").val(), editTime : $("#target #editTime").val() } $.ajax({ url: 'http://localhost:8088/documents/updateDocument', type : "POST", contentType : "application/json", data : JSON.stringify(formData), dataType : 'json', success : function(result) { }, error: function() { window.location = "/error"; console.log('error', arguments); }, complete: function() { console.log('complete', arguments); } }).done(function() { window.location = "/documents"; console.log('done', arguments); }); }); 

Update

I've just tried to switch off Spring Security and POST method became accessible. Here is the authorization features:

 protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/resources/**", "/signup", "/about").permitAll() .antMatchers("/administrator/**").hasRole("ADMIN") .antMatchers("/users/**").hasRole("ADMIN") .antMatchers("/documents/**").hasRole("ADMIN") .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") .anyRequest().authenticated() .and() .formLogin() .and() .logout().logoutSuccessUrl("/login?logout") .and() .exceptionHandling().accessDeniedPage("/403") // .and() // .csrf() ; } 

Update

I try to switch on SpringSecurity but disable CSRF .csrf().disable(). After that POST methods work. I think that disabling the CSRF is not a good idea. This may lead to XSS attacks. So I should configure CSRF-token generation and its interchange.

10
  • 2
    have you tried hitting your rest api via postman? What's the output then? Commented Oct 17, 2018 at 6:56
  • @Rinat can you check the logs, whether it shown "no mapping found for this url" , also please check after setting context path and try hitting with the new url, this link myt be helpful : stackoverflow.com/questions/40670654/… Commented Oct 17, 2018 at 7:04
  • @tryingtolearn output is: {"timestamp":"2018-10-17T07:08:26.074+0000","status":404,"error":"Not Found","message":"No message available","path":"/documents/updateDocuments"} Commented Oct 17, 2018 at 7:12
  • It means there is a context path set in your application which needs to be appended in each request. Are you using springboot? If yes , check for context path property in application.properties Commented Oct 17, 2018 at 7:14
  • @tryingtolearn only server.port and spring.datasource.url, spring.datasource.password, spring.datasource.username in my application.properties file. GET REST endpoints work, but POST not ((( Commented Oct 17, 2018 at 7:23

3 Answers 3

1

1) Please check spring startup logs : you can get the url initialized for the project

2) try putting context path for the application,

these link myt be helpful : in spring MVC : How to Set Context root in Spring Mvc Project in spring boot : Add context path to Spring Boot application

sample url :

http://localhost:8088/{contextPath}/documents/updateDocument

3) better use API documentation like swagger ui, you can try hitting the url easly and get the available urls in your controller classes.

Sign up to request clarification or add additional context in comments.

Comments

0

you need add projectName to addres eg : http://localhost:8088/projectName/documents/updateDocument

Comments

0

I think you are missing @Autowired annotation in your code. Try adding this annotation before Constructor and try again. @Autowired public DocumentResources(DocumentsMapper mapper){this.mapper = mapper;}

1 Comment

the context would have failed if OP'd had problems with injection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.