Method with @PathVariable In SpringBoot returns empty response

I am trying to write a method that takes a @PathVariable parameter and redirects the 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 to http: // localhost: 8080 / SpringBlog / user / 5 It shows me an empty response. (Nothing even in the page source)

When I looked at the Spring output console, it shows me the following message when I try to navigate:

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

osboot.web.support.ErrorPageFilter

Unable to forward error page for request [/ user / 5] because the response has already been completed. As a result, the response may have an incorrect status code. If your application is running on WebSphere Application Server, you can fix this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

I have already tried the following parameter descriptions in the following cases:

@PathVariable (value = "customerId") CustomerId string

@PathVariable (name = "customerId") CustomerId string

@PathVariable ("customerId") String customerId

@PathVariable String customerId

None of them worked, always an empty answer with the same error message.

I am sure all files are in the correct place, in my MainController class I have several methods with No Parameters, RequestParams, etc., they all work as expected. But if I want to create a RequestMapping with @PathVariable it always returns an empty response and the same error message in the output console.

But if I try the same approach with @RestController, it works 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

      

Thank you for your help.

+3


source to share


3 answers


Please check your application.properties file ...

prefix and suffix



spring.mvc.view.prefix: (Where are jsp files) Example /WEB-INF/ or / 
spring.mvc.view.suffix: .jsp

      

0


source


Annotation is @RestController

automatically added @ResponseBody

to your methods.

What it @ResponseBody

is about binding the outgoing return value to the body of the HTTP response using HttpMessageConverter

. If you don't add the annotation @RestController

or @ResponseBody

, then Spring will try to resolve this for the view, usually JSP pages.

So, in your case, Spring is trying to find a match of the view "Hello"+customerId

instead of printing the result "Hello"+customerId

.



So you are using the @PathVariable annotation correctly. :)

You can read here

+1


source


If you are using annotation @Controller

, you need to add an annotation @ResponseBody

to bind the outgoing return value to the body of the HTTP response. So your c code @Controller

should look like this:

@Controller
public class MainController 
{

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

      

0


source







All Articles