Ashu
Ashu

Reputation: 2266

@RequestMapping Not working in Spring Version 1.5.2.RELEASE

I am trying to run a spring boot project. While I choose release 1.5.2 the requestMapping urls are not working. its giving me 404 error always.

Can somebody please confirm me if any tags are missing or any setting change in version. or any other thins which i missed.

POM.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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.prizy</groupId>
    <artifactId>ProductApi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ProductApi</name>
    <description>Project for Product Managment</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Controller:

package com.prizy.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.prizy.model.Product;
import com.prizy.service.ProductService;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/products")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    @RequestMapping("/hi")
    public String getString() {
        return "Hi";
    }

    @RequestMapping("/products/{productId}")
    public Product getProduct(@PathVariable int productId){     
        return productService.getTopic(productId);
    }

    @RequestMapping(method=RequestMethod.POST, value="/products")
    public void addProduct(@RequestBody Product product) {
        productService.addProduct(product);
    }

    @RequestMapping(method=RequestMethod.PUT, value="/products/{productId}")
    public void updateProduct(@RequestBody Product product, @PathVariable int productId) {
        productService.updateProduct(product,productId);
    }

    @RequestMapping(method=RequestMethod.DELETE, value="/products/{productId}")
    public void deleteProduct(@PathVariable int productId) {
        productService.deleteProduct(productId);
    }

}

Main Class is

package com.prizy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@EnableAutoConfiguration
@SpringBootApplication
public class ProductApiApplication {

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

Project Structure

Note: Question updated with more information.

Upvotes: 0

Views: 741

Answers (2)

Ashu
Ashu

Reputation: 2266

After trying all the scenario I have finally changed the Spring boot version to 1.3.2.RELEASE

After doing this its working fine. I am still trying to get the answer why its not working on 1.5.2 Release.

But to resolve this error, i did below change to my POM.xml file

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

After this change , I can see the request mapping urls in logs too:

2017-04-19 11:10:49.785  INFO 4236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.epic.controllers.TestController.sayHello()
2017-04-19 11:10:49.786  INFO 4236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/helloss]}" onto public java.lang.String com.epic.TestControllerTest.sayHello()
2017-04-19 11:10:49.789  INFO 4236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2

Please note that meanwhile I changed the mapping urls names so it may not be same as the question.

Upvotes: 0

Anshul Sharma
Anshul Sharma

Reputation: 3522

check the web.xml file is available or not into src>main>webapp>WEB-INF>web.xml. if not available the craete this file and use the below code.

 <servlet>
    <servlet-name>myServlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

Upvotes: 1

Related Questions