I am trying to create webservices in JAVA Spring boot with backend as SQL Server 2012. I followed the tutorial from this website https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/ .
The only change i did was, changed the application.properties as
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=SampleSpringDB
spring.datasource.username=sa
spring.datasource.password=sqlpassword
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto = create-drop
Tomcat does not show any errors while running the app.
While testing POST method in Postman i get error in postman as
{
"timestamp": "2018-11-21T04:44:06.474+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/notes/"
}
The code is totally copied from the above mentioned site.
Code for Controller
@RestController
@RequestMapping(value="/api", produces="application/json")
public class NoteController {
@Autowired
NoteRepository noteRepository;
// Create Note
@PostMapping("/notes")
public Note createNote(@Valid @RequestBody Note note) {
return noteRepository.save(note);
}
// Get all notes
@GetMapping("/notes")
public List<Note> getAllNotes() {
return noteRepository.findAll();
}
// Get Single Note
@GetMapping("/notes/{id}")
public Note getNoteById(@PathVariable(value = "id") Long noteID) {
return noteRepository.findById(noteID).orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteID));
}
// Update Note
@PutMapping("/notes/{id}")
public Note updateNote(@PathVariable(value = "id") Long noteID, @RequestBody Note noteDetails) {
Note note = noteRepository.findById(noteID)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteID));
note.setTitle(noteDetails.getTitle());
note.setContent(noteDetails.getContent());
Note updatedNote = noteRepository.save(note);
return (updatedNote);
}
// Delete Note
@DeleteMapping("/notes/{id}")
public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteID) {
Note note = noteRepository.findById(noteID)
.orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteID));
noteRepository.delete(note);
return ResponseEntity.ok().build();
}
}
POSTMAN POST url
localhost:8080/api/notes/ body params as {"title":"test","content":"test"}
Can you do a quick check whether you added @SpringBootApplication
:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication includes @ComponentScan which scans the package it's in and all children packages. Your controller may not be in any of them.
Did you added sqlserver connector at pom.xml ? If added please skip this answer and I will remove this. I tested your code on mysql server its working fine.
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
This issue has been fixed (In a Weird way) I created the same project with backend as MySQL and tested out the service. It worked fine. Then i added SQL server dependency
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
into pom.xml and changed the application.properties to
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=SampleSpringDB
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driverClassName=
com.microsoft.sqlserver.jdbc.SQLServerDriver spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=
org.hibernate.dialect.SQLServer2012Dialect spring.jpa.hibernate.ddl-auto
= create-drop
By doing so it worked out fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.