9

While running springboot restservice application got 404 error. I am using spring boot, jersey rest. I have tried GET requests http://localhost:8080/dunames but not able to resolve. Please help.

Model Class:

@Entity
@Table(name = "du")
public class duname {


    private String duname;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id; 
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public duname()
    {

    }

    public duname(String duname, int id)
    {
    this.duname=duname; 
    this.id=id;
    }


    public String getDuname() {
        return duname;
    }

    public void setDuname(String duname) {
        this.duname = duname;
    }
}

Service Class:

public class duservice {

    @Autowired
    private durepo durepository;

    public List<duname> getAlldu()
    {
        List<duname> dunames=new ArrayList<duname>();
        durepository.findAll()
        .forEach(dunames::add);
        return dunames;
    }

    public void addDu(duname dunames)
    {
        durepository.save(dunames);
    }

    public duname getDu(int id)
    {
        return durepository.findOne(id);
    }

    public void deleteDu(int id)
    {
        durepository.delete(id);
    }

    public void updateDu(int id, duname dunames)
    {
        durepository.save(dunames);
    }
}

Controller class:

    @RestController
public class ducontroller {


    @Autowired
    private duservice duService;

    private final Logger log=(java.util.logging.Logger) LoggerFactory.getLogger(ducontroller.class);


    @RequestMapping("/dunames")
    public List<duname> getAlldu()
    {
          log.info("Starting");
    return duService.getAlldu();
    }

    @RequestMapping("/duname/{id}")
    public duname getdu(@PathVariable int id)
    {
        return duService.getDu(id);
    }

    @RequestMapping(method=RequestMethod.POST,value="/dunames")
    public void addDuname(@RequestBody duname dunames)
    {
        duService.addDu(dunames);
    }

Repository:

public interface durepo extends CrudRepository<duname,Integer> {


}

Main Class:

 @SpringBootApplication
public class DuaddApplication {

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

Pom File:

 <?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.du</groupId>
    <artifactId>duadd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>duadd</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>


</project>

Application Properties:

spring.datasource.url=jdbc:postgresql://localhost:2280/todo
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.generate-ddl=true
server.port=8080 

Some console output:

2017-10-26 11:06:08.952  INFO 4552 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-10-26 11:06:08.975  INFO 4552 --- [           main] com.soprasteria.du.DuaddApplication      : Started DuaddApplication in 7.096 seconds (JVM running for 7.633)
2017-10-26 11:06:32.779  INFO 4552 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-10-26 11:06:32.779  INFO 4552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-10-26 11:06:32.800  INFO 4552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 21 ms
11
  • Yes i know but can't figure out where i went wrong. Commented Oct 26, 2017 at 6:50
  • According to your console output, it seems that the method getAlldu() didn't be called. So, did you get any message after invoking http://localhost:8080/dunames? Commented Oct 26, 2017 at 8:32
  • What does your web.xml look like? Commented Oct 26, 2017 at 22:40
  • @LHCHIN Yes it is not called. I got the 404 message. Commented Oct 27, 2017 at 4:59
  • @Wabi It does not have web.xml . It is spring boot application Commented Oct 27, 2017 at 5:00

4 Answers 4

33

I got the solution. It was due to package visibility. Main class was not able to find package in which controller class is present. So, I added all classes under the same package. You can also place application class one level up.

Got help from below link:

Spring Boot: Cannot access REST Controller on localhost (404)

Sign up to request clarification or add additional context in comments.

Comments

1

A lot of people said file management but I found my issue to be not including a path for my @GetMapping

For example, instead of @GetMapping I had: @GetMapping("/")

This issue held me back so much and though might not solve yours's, I'm sure and hoping will help someone. Thanks

Comments

0

There is serval reason associated with error 404, in my case I created a spring boot starter project in eclipse and forget to include spring-boot-starter-web in my project .I solved the issue after adding dependency in pom.xml

org.springframework.boot spring-boot-starter-web

Comments

0

For me it was a missing @RestController above my controller class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.