Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method With @PathVariable In SpringBoot Returns Empty Response

Tags:

spring-boot

I'm trying to write a method which takes a @PathVariable parameter and redirects user to a jsp file.

@Controller
public class MainController 
{

    @RequestMapping("/user/{customerId}")
    // http://localhost:8080/Test/user/5
    public String getCustomerById(@PathVariable("customerId") String customerId, Model model) 
    {
            model.addAttribute("customer_id", customerId);
            // this is the user_details.jsp file, I need to show this jsp file to visitor
            return "user_details";
    }
}

When I try to navigate http://localhost:8080/SpringBlog/user/5 It's showing me an empty response. (Nothing Even In Page Source)

When I looked into Spring output console, it's showing me the following message when I'm trying to navigate :

2017-07-19 13:24:56.191 ERROR 6772 --- [io-8080-exec-75]

o.s.boot.web.support.ErrorPageFilter

Cannot forward to error page for request [/user/5] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

I've already tried following parameter descriptions as the followings :

@PathVariable(value="customerId") String customerId

@PathVariable(name="customerId") String customerId

@PathVariable("customerId") String customerId

@PathVariable String customerId

None of them worked, always empty response with same error message.

I'm sure that all files are in correct place, in my MainController Class I have several methods with No Parameters, RequestParams, etc.. all of them working as expected. But if I want to create a RequestMapping with @PathVariable, it always returns empty response and same error message in the output console.

But if I try same approach with @RestController it's working as expected:

@RestController
public class RestApi
{
    // http://localhost:8080/Test/api/user/56
    // Is Working, Returns Hello 56 As Response
    @RequestMapping("api/user/{customerId}")
    public String apiTest(@PathVariable("customerId") String customerId)
    {
        return "Hello "+customerId; 
    }
}

What am I missing ?

Application Details :

<packaging>war</packaging>
...
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
</parent>
...

Apache Tomcat/7.0.56
JVM 1.8.0_131-b11

Thanks for your help.

like image 770
Emir Civas Avatar asked Sep 09 '26 05:09

Emir Civas


1 Answers

The annotation @RestController automatically adds the @ResponseBody to your methods.

What @ResponseBody does is to bind the outgoing returned value to the HTTP response body using a HttpMessageConverter. If you don't add either the @RestController or @ResponseBody annotation then Spring will try to resolve that to a view, commonly a JSP page.

So in your case Spring is trying to find the view matchin "Hello"+customerId instead of printing out the result of "Hello"+customerId.

So you're using the @PathVariable annotation correctly. :)

You can read more here

like image 158
Daniel Figueroa Avatar answered Sep 13 '26 14:09

Daniel Figueroa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!