3

I'm having a problem with my spring boot mysql project, the controller class works just find for METHOD GET(get all), but I can't seem to post and get error 405: Method "POST" Not Allowed

Heres my controller class:

 package com.example.demo.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.demo.Blog;
import com.example.demo.repository.BlogRespository;

import java.util.List;
import java.util.Map;

@RestController
public class BlogController {

    @Autowired
    BlogRespository blogRespository;

    @GetMapping("/blog")
    public List<Blog> index(){
        return blogRespository.findAll();
    }

    @GetMapping("/blog/{id}")
    public Blog show(@PathVariable String id){
        int blogId = Integer.parseInt(id);
        return blogRespository.findById(blogId)
                 .orElseThrow(() -> new IllegalArgumentException(
                 "The requested resultId [" + id +
                 "] does not exist."));
    }

    @PostMapping("/blog/search")
    public List<Blog> search(@RequestBody Map<String, String> body){
        String searchTerm = body.get("text");
        return blogRespository.findByTitleContainingOrContentContaining(searchTerm, searchTerm);
    }

    @PostMapping("/blog")
    public Blog create(@RequestBody Map<String, String> body){
        String title = body.get("title");
        String content = body.get("content");
        return blogRespository.save(new Blog(title, content));
    }

    @PutMapping("/blog/{id}")
    public Blog update(@PathVariable String id, @RequestBody Map<String, String> body){
        int blogId = Integer.parseInt(id);
        // getting blog
        Blog blog = blogRespository.findById(blogId)
             .orElseThrow(() -> new IllegalArgumentException(
             "The requested resultId [" + id +
             "] does not exist."));
        blog.setTitle(body.get("title"));
        blog.setContent(body.get("content"));
        return blogRespository.save(blog);
    }


    @DeleteMapping("blog/{id}")
    public boolean delete(@PathVariable String id){
        int blogId = Integer.parseInt(id);
        blogRespository.delete(blogId);
        return true;
    }


}

and heres my repository class if you need it

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.Blog;

import java.util.List;

@Repository
public interface BlogRespository extends JpaRepository<Blog, Integer> {

    // custom query to search to blog post by title or content
    List<Blog> findByTitleContainingOrContentContaining(String text, String textAgain);

}

Im trying the POST request with SoapUI, and just cant seem to find the solution, many thanks

Drason
  • 41
  • 1
  • 8

5 Answers5

3

The method post will be not Allowed if you have csrf configured or enabled then your need to provided a valid csrf while posting your form or data

Check your spring security config for this For example

    @Configuration
    @EnableWebSecurity
    @ComponentScan(basePackageClasses = CustomUserDetailsService.class)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    .....

RequestMatcher csrfRequestMatcher = new RequestMatcher() {
        // Enabled CSFR protection on the following urls:
        //@formatter:off
        private AntPathRequestMatcher[] requestMatchers = 
            {
                new AntPathRequestMatcher("/**/verify"),
                        new AntPathRequestMatcher("/**/login*")
            };
        //@formatter:off

        @Override
        public boolean matches(final HttpServletRequest request) {
            // If the request match one url the CSFR protection will be enabled
            for (final AntPathRequestMatcher rm : requestMatchers) {
                if (rm.matches(request)) {
                    System.out.println();
                    /* return true; */
                }
            }
            return false;
        } // method matches
    };
@Override
    protected void configure(final HttpSecurity http) throws Exception {
        //@formatter:off

        http.headers().frameOptions().sameOrigin()
        .and()
        .authorizeRequests()
        .antMatchers("/","/css/**", "/static/**", "/view/**", "**/error/**").permitAll()
        .anyRequest().authenticated()
        .and()
        .formLogin().loginPage("/mvc/login").permitAll() 
        .authenticationDetailsSource(authenticationDetailsSource())
        .successHandler(authenticationSuccessHandler)
        .usernameParameter("username").passwordParameter("password")
        .and()
        .logout().permitAll()
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
        .addLogoutHandler(customLogoutHandler)
        .logoutSuccessHandler(customLogoutSuccessHandler)
        .logoutSuccessUrl("/login?logout")
        .and()
        .exceptionHandling()
        .accessDeniedPage("/403")
                .and()
                .csrf()/* .requireCsrfProtectionMatcher(csrfRequestMatcher) */
        .ignoringAntMatchers("/crud/**","/view/**")
    ;
        // @formatter:off


    }

Thanks

adia
  • 404
  • 2
  • 8
2

You may wish to consider the consumes attribute on the search method to inform spring which Content-Type you expect the method to consume. e.g. @PostMapping(value="/blog/search", consumes=org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE).

Take a look at implementations of org.springframework.http.converter.HttpMessageConverter. Something like the org.springframework.http.converter.FormHttpMessageConverter impl will convert a request body into a MultiValueMap<String,?>

You can also follow this example: Spring MVC - How to get all request params in a map in Spring controller? which uses the @RequestParam annotation instead of @RequestBody.

Can you post a sample curl request that demonstrates the HTTP 405 response - I presume you are posting to the /blog/search endpoint?

David
  • 7,652
  • 21
  • 60
  • 98
  • it says that APPLICATION_FORM_URLENCODED cannot be resolved to a variable – Drason Jun 08 '18 at 10:00
  • Try importing the constant: `org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED` – David Jun 08 '18 at 10:14
  • I can import org.springframework.http.MediaType, but cant import org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED, says it doesnt exist – Drason Jun 08 '18 at 10:25
  • `APPLICATION_FORM_URLENCODED` is the constant, you'd either `import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE` Or you'd just reference it like this: `@PostMapping(value="/blog/search", consumes=org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE)` as the consumes attribute takes the string variation. – David Jun 08 '18 at 10:30
  • it shows no errors now, but still says that post is not allowed :\ @PostMapping(value="/blog/search", consumes=org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE) public List search(@RequestBody Map body){ String searchTerm = body.get("text"); return blogRespository.findByTitleContainingOrContentContaining(searchTerm, searchTerm); } – Drason Jun 08 '18 at 10:37
  • what is it that says "post is pot allowed". I'd be useful to see a sample curl request. – David Jun 08 '18 at 12:04
0

I tried to reproduce the issue by just writing a dummy code, but it is working perfectly fine for me.

please find below the code snippet tried by me -

package com.pradeep.rest.controller;

import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestRequestController {

    @GetMapping("/blog")
    public String show() {
        String result = "Hello from show";
        return result;
    }

    @PostMapping("/blog")
    public String create(@RequestBody Map<String, String> body) {
        String title = body.get("title");
        String content = body.get("content");
        String result = "title= " + title + " : content= " + content;
        return result;
    }
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.pradeep.rest</groupId>
    <artifactId>RestApi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- to ease development environment -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

Input and Output snippet:

enter image description here

Pradeep
  • 12,309
  • 3
  • 20
  • 25
0

My trial worked perfectly Postman

And this is my controller. I followed this tutorial Spring Boot Angular

package io.crzn.myNotes.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.crzn.myNotes.exception.ResourceNotFoundException;
import io.crzn.myNotes.model.MyNotes;
import io.crzn.myNotes.repository.myNotesRepository;

@RestController
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping("/api/v1")
public class myNotesController {

    @Autowired
    private myNotesRepository mynotesRepository;

    @GetMapping("/mynotes")
    public List<MyNotes> getAllmyNotes(){
        return mynotesRepository.findAll();
    }

    @GetMapping("/mynotes/{id}")
    public ResponseEntity<MyNotes> getEmployeeById(@PathVariable(value = "id") Long mynotesId)
        throws ResourceNotFoundException{
        MyNotes mynotes = mynotesRepository.findById(mynotesId)
                .orElseThrow(() -> new ResourceNotFoundException("Note not found for this id : :" + mynotesId));
        return ResponseEntity.ok().body(mynotes);
    }

    @PostMapping("/mynotes")
    public MyNotes createMyNotes(@Valid @RequestBody MyNotes mynotes) {
        return mynotesRepository.save(mynotes);
    }

    @PutMapping("/mynotes/{id}")
    public ResponseEntity<MyNotes> updateMyNotes(@PathVariable(value = "id") Long mynotesId,
            @Valid @RequestBody MyNotes mynotesDetails)
                    throws ResourceNotFoundException{
        MyNotes mynotes = mynotesRepository.findById(mynotesId)
                .orElseThrow(() -> new ResourceNotFoundException("Not not found for this id : : " + mynotesId));

        mynotes.setstatus(mynotesDetails.getstatus());
        mynotes.setbody(mynotesDetails.getbody());
        mynotes.settitle(mynotesDetails.gettitle());
        final MyNotes updatedMyNotes = mynotesRepository.save(mynotes);
        return ResponseEntity.ok(updatedMyNotes);
    }

    @DeleteMapping("/mynotes/{id}")
    public Map<String, Boolean> deleteMyNotes(@PathVariable(value = "id") Long mynotesId)
            throws ResourceNotFoundException{
        MyNotes mynotes = mynotesRepository.findById(mynotesId)
                .orElseThrow(() -> new ResourceNotFoundException("Not not found for this id : : " + mynotesId));

        mynotesRepository.delete(mynotes);
        Map<String, Boolean> response = new HashMap<>();
        response.put("deleted", Boolean.TRUE);
        return response;

    }




}
0

I went through the same error where I was able to make a GET request while the POST method was not allowed And later I found that in staging server SSL was enabled so just changed "http" to "https" and made it work

Dila Gurung
  • 1,726
  • 21
  • 26