Rudziankoŭ
Rudziankoŭ

Reputation: 11251

RequestMapping GET and POST methods handling in Spring REST

I have such code:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @RequestMapping(value = "/foo", method = RequestMethod.POST)
    String testPost(@RequestParam("param1") String param1, @RequestParam("param2") String param2) {
        return param1 + param2;
    }

    @RequestMapping("/foo")
    String testGet(@RequestParam String param1) {
        return param1;
    }
}

And I execute such curl expressions:

curl --verbose http://localhost:9000/foo?param1=Sergei
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9000 (#0)
> GET /foo?param1=Sergei HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:9000
> Accept: */*
> 
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 29 Dec 2015 08:55:56 GMT
< 
* Connection #0 to host localhost left intact
{"timestamp":1451379356514,"status":404,"error":"Not Found","message":"No message available","path":"/foo"}

and,

curl --verbose --data "param1=value1&param2=value2" http://localhost:9000/foo
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9000 (#0)
> POST /foo HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:9000
> Accept: */*
> Content-Length: 27
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 27 out of 27 bytes
< HTTP/1.1 405 Method Not Allowed
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Allow: HEAD, GET
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 29 Dec 2015 09:01:46 GMT
< 
* Connection #0 to host localhost left intact
{"timestamp":1451379706832,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/foo"}

Help me please get both my methods work.

Upvotes: 1

Views: 361

Answers (2)

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5659

You should change the scope level of the two controller methods to public. Right now, they don't have any, so the methods are package local by default.

public String testPost(@RequestParam("param1") String param1, @RequestParam("param2") String param2) {

public String testGet(@RequestParam String param1) {

Upvotes: 1

Ali Dehghani
Ali Dehghani

Reputation: 48123

Add a RestController or Controller stereotype annotation to your Application class like this:

@RestController
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
...
}

Note: You can use SpringBootApplication meta annotation instead of those three, so you would have:

@RestController
@SpringBootApplication
public class Application {
....
} 

Upvotes: 1

Related Questions