Reputation: 73
Its a simple code but there is something that i am missing that i am not getting, the app is running but the API is not working. while running the code it is not logging the exposed api details. Its not debugging the mapped api in using RequestMappingHandlerMapping while debuging. Could you help me on this
Controller.java :
package com.mindtree.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@RequestMapping("/find")
@ResponseBody
public ResponseEntity<String> findEmployee(){
String emp;
emp="id is: ";
return new ResponseEntity<String>(emp,HttpStatus.OK);
}
}
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.mindtree</groupId>
<artifactId>mydemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mydemo1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Mydemo1Application.java :
package com.mindtree.mydemo1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
public class Mydemo1Application {
public static void main(String[] args) {
SpringApplication.run(Mydemo1Application.class, args);
}
}
@Configuration
@EnableSwagger2
class SwaggeConfiguration {
}
Application.properties :
server.port=8085
Upvotes: 0
Views: 143
Reputation: 1059
it caused by base package of component scan. should refer best practice of spring boot package layout and it said,
We generally recommend that you locate your main application class in a root package above other classes. The @SpringBootApplication annotation is often placed on your main class, and it implicitly defines a base “search package” for certain items.
this mean you should move your controllers package to com.mindtree.mydemo1.controllers
if you want to define com.mindtree.mydemo1
as an base package
Upvotes: 1