I have created a simple Spring Boot application using REST APIs. I am using the following controller:
package me.hiboy.SpringBootRESTAPI;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BlogController {
@Autowired
BlogRepository blogRepository;
@GetMapping("/blog")
public List<Blog> index() {
return blogRepository.findAll();
}
@GetMapping("/blog/{id}")
public Blog show(@PathVariable String id) {
int blogId=Integer.parseInt(id);
return blogRepository.findOne(blogId);
}
@PostMapping("/blog/search")
public List<Blog> search(@RequestBody Map<String, String> body) {
String searchTerm=body.get("text");
return blogRepository.findByTitleContainingOrContentContaining(searchTerm, searchTerm);
}
@PostMapping("/blog")
public Blog create(@RequestBody Map<String, String> body) {
String title=body.get("title");
String content=body.get("content");
return blogRepository.save(new Blog(title, content));
}
@PutMapping("/blog/{id}")
public Blog update(@PathVariable String id, @RequestBody Map<String, String> body) {
int blogId=Integer.parseInt(id);
Blog blog=blogRepository.findOne(blogId);
blog.setTitle(body.get("title"));
blog.setContent(body.get("content"));
return blogRepository.save(blog);
}
@DeleteMapping("/blog/{id}")
public boolean delete(@PathVariable String id) {
int blogId=Integer.parseInt(id);
blogRepository.delete(blogId);
return true;
}
}
And the following Entity:
package me.hiboy.SpringBootRESTAPI;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Blog {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String title;
private String content;
public Blog() {}
public Blog(String title, String content) {
this.setTitle(title);
this.setContent(content);
}
public Blog(int id, String title, String content) {
this.setId(id);
this.setTitle(title);
this.setContent(content);
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public void setId(int id) {
this.id=id;
}
public void setTitle(String title) {
this.title=title;
}
public void setContent(String content) {
this.content=content;
}
@Override
public String toString() {
return "Blog{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
However, for some reason, I am unable to get any response when I visit localhost (I get the 404 response). Could someone please point out what I must be missing? It is a simple tutorial which I am following this tutorial and the other files that I have built include: pom.xml
, application.properties
, database.sql
, App.java
(which has my main() function, and the BlogRepository.java
.
Thanks!
Edit:
App.java is as below:
package me.hiboy.SpringBootRESTAPI;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@ComponentScan({"com.delivery.request"})
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
BlogRepository.java is as below:
package me.hiboy.SpringBootRESTAPI;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BlogRepository extends JpaRepository<Blog, Integer> {
List<Blog> findByTitleContainingOrContentContaining(String text, String textAgain);
}
Edit:
My application.properties is as follows:
#spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://mysql-standalone:3306/restapi
spring.datasource.username=sa
spring.datasource.password=<my_password_here>
Uploading screenshot of the error:
Edit: While I have completely understood the tutorial, what I fail to understand is the annotation: @ComponentScan({"com.delivery.request"})
. If this is not included, then I get an error: Consider defining a bean of type 'me.hiboy.SpringBootRESTAPI.BlogRepository' in your configuration.
The tutorial that I followed does not include this annotation and I figured this out by following some SO posts. I wonder what I did differently so as to necessitate a requirement for this annotation. If you could point out, it would be a great help towards my learning and I would highly appreciate it!
Edit: I have uploaded the entire code on GitHub. It is a fairly simple and straight forward app, but I don't know where I am going wrong! :(
Also, as per my earlier question here, I am not sure about the location of the resources
folder. Currently, my directory structure is as follows:
I am doubtful about this, because all this issue I describe here only started after I integrated the MySQL database with this application. If I simply hard code the values, then the API queries work fine (I don't get a 404). I am also doubtful because although I mention about port 8086
in the application.properties
file, it (the WhiteLabel error) still starts on the port 8080
. Thanks!