无法在Spring Boot中发起POST请求

时间:2018-11-21 05:05:26

标签: java hibernate spring-boot sql-server-2012

我正尝试在后端为SQL Server 2012的JAVA Spring引导中创建Web服务。我按照此网站https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/上的教程进行操作。

我所做的唯一更改是将application.properties更改为

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在运行应用程序时未显示任何错误。

在邮递员中测试POST方法时,我在邮递员中收到错误消息

{
"timestamp": "2018-11-21T04:44:06.474+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/notes/"
}

代码是从上述站点完全复制的。

控制器代码

    @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网址

localhost:8080 / api / notes / 正文参数为{“ title”:“ test”,“ content”:“ test”}

3 个答案:

答案 0 :(得分:0)

您能否快速检查是否添加了@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包含@ComponentScan,它可以扫描其中的包以及所有子包。您的控制器可能不在其中任何一个中。

答案 1 :(得分:0)

您是否在 pom.xml 中添加了sqlserver连接器?如果添加,请跳过此答案,我将删除它。我在mysql服务器上测试了您的代码,效果很好。

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>
</dependency>

答案 2 :(得分:0)

此问题已修复(很奇怪) 我使用MySQL创建了与后端相同的项目,并测试了该服务。工作正常。然后我添加了SQL Server依赖项

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>

进入pom.xml并将application.properties更改为

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

这样做可以很好地解决问题。