简体   繁体   English

更新和删除不适用于 Spring Boot 应用程序

[英]Update and Delete not working on Spring Boot Application

I was trying to create a simple CRUD spring boot application after watching an online course.观看在线课程后,我试图创建一个简单的 CRUD spring boot 应用程序。 But the update and delete functions are not working.但是更新删除功能不起作用。 GET and POST is working, but PUT and DELETE is not working. GETPOST有效,但PUTDELETE无效。 I have inserted screenshots for the postman response at the end.我在最后插入了邮递员回复的截图。 I'm getting 200 OK response on postman but, why is it not working?我在邮递员那里得到了 200 OK 的回复,但是为什么它不起作用? Also, I'm using PostgreSQL for database.另外,我使用 PostgreSQL 作为数据库。 Please help.请帮忙。

The session controller ( delete and update are the last two methods) :会话控制器(删除更新是最后两个方法):

package com.organizer.conferencedemo.controllers;

import com.organizer.conferencedemo.models.Session;
import com.organizer.conferencedemo.repositories.SessionRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/v1/sessions")
public class SessionsController {

    @Autowired
    private SessionRepository sessionRepository;

    @GetMapping
    public List<Session> list() {
        return sessionRepository.findAll();
    }

    @GetMapping
    @RequestMapping("{id}")
    public Session get (@PathVariable Long id) {
        return sessionRepository.getOne(id);
    }

    @PostMapping
    public Session create(@RequestBody final Session session) {
        return sessionRepository.saveAndFlush(session);
    }

    @RequestMapping(value = {"id"}, method = RequestMethod.DELETE)
    public void delete(@PathVariable Long id)
    {

        sessionRepository.deleteById(id);
    }

    @RequestMapping(value = {"id"}, method = RequestMethod.PUT)
    public Session update(@PathVariable Long id, @RequestBody Session session)
    {
        Session existingSession = sessionRepository.getOne(id);
        BeanUtils.copyProperties(session, existingSession, "session_id");
        return sessionRepository.saveAndFlush(existingSession);
    }
}

Session repository:会话存储库:

package com.organizer.conferencedemo.repositories;

import com.organizer.conferencedemo.models.Session;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SessionRepository extends JpaRepository<Session, Long> {
}

application.properties:应用程序属性:

spring.datasource.url=jdbc:postgresql://localhost:5432/conferencedb
spring.datasource.username=
spring.datasource.password=
spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = none
spring.jpa.hibernate.show-sql = true

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.continue-on-error=true

pom.xml: 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.organizer</groupId>
    <artifactId>conference-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>conference-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </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>
        </plugins>
    </build>

</project>

Build comments:构建注释:

:: Spring Boot ::       (v2.1.10.RELEASE)

2020-01-26 16:36:58.221  INFO 10272 --- [           main] c.o.c.ConferenceDemoApplication          : Starting ConferenceDemoApplication on hp with PID 10272 (started by User in C:\Users\User\Documents\1. PERSONAL\8. IND PROJECTS\conference-demo\conference-demo)
2020-01-26 16:36:58.264  INFO 10272 --- [           main] c.o.c.ConferenceDemoApplication          : No active profile set, falling back to default profiles: default
2020-01-26 16:37:05.282  INFO 10272 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-01-26 16:37:05.979  INFO 10272 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 580ms. Found 2 repository interfaces.
2020-01-26 16:37:10.887  INFO 10272 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$8550c927] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-01-26 16:37:14.000  INFO 10272 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-01-26 16:37:14.550  INFO 10272 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-26 16:37:14.551  INFO 10272 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2020-01-26 16:37:15.215  INFO 10272 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-26 16:37:15.216  INFO 10272 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 16441 ms
2020-01-26 16:37:28.898  INFO 10272 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-01-26 16:37:41.464  INFO 10272 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-01-26 16:37:44.305  INFO 10272 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2020-01-26 16:37:46.371  INFO 10272 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.13.Final}
2020-01-26 16:37:46.382  INFO 10272 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2020-01-26 16:37:48.374  INFO 10272 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2020-01-26 16:37:49.043  INFO 10272 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2020-01-26 16:37:50.035  INFO 10272 --- [           main] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true
2020-01-26 16:37:50.070  INFO 10272 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@571a01f9
2020-01-26 16:38:01.918  INFO 10272 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-01-26 16:38:09.233  INFO 10272 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-26 16:38:09.364  WARN 10272 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-01-26 16:38:10.388  INFO 10272 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-26 16:38:10.394  INFO 10272 --- [           main] c.o.c.ConferenceDemoApplication          : Started ConferenceDemoApplication in 79.401 seconds (JVM running for 85.94)
2020-01-26 16:38:37.417  INFO 10272 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-26 16:38:37.417  INFO 10272 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-26 16:38:37.436  INFO 10272 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 19 ms

create new session :创建新会话

创建新会话 ] ]

update that session : After update the response should be with updated session name and description.更新该会话:更新后,响应应包含更新的会话名称和描述。 But it's showing the old previous session name and description.但它显示了以前的旧会话名称和描述。

更新那个会话 ] ]

delete that session :删除该会话

For delete it shouldn't return anything.对于删除它不应该返回任何东西。 But it's returning details of that session id.但它正在返回该会话 ID 的详细信息。

删除那个会话

You have to enclose path variable parameters in the brackets in you mapping annotation in order this to work, otherwise PUT and DELETE requests wouldn't be matching and therefore ignored.您必须在映射注释中将路径变量参数括在括号中才能使其工作,否则PUTDELETE请求将不会匹配并因此被忽略。

Eg:例如:

@RequestMapping(value = "{id}", method = RequestMethod.PUT)
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM