package com.shashank.topic;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Topic {
@Id
private String id;
private String name;
private String description;
public Topic() {
}
public Topic(String id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
存储库是这样配置的
package com.shashank.topic;
import org.springframework.data.repository.CrudRepository;
public interface TopicRepository extends CrudRepository<Topic, String> {
/***
* CURD OPERATIONS
* getAllTopics()
* getTopic(String id)
* updateTopic(Topic topic)
* deleteTopic(String id)
* */
}
这是另一个类
package com.shashank.topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class TopicService {
@Autowired
private TopicRepository topicRepository;
public List<Topic> getAllTopics() {
List<Topic> topics = new ArrayList<>();
topicRepository.findAll().forEach(topics::add);
return topics;
}
public Optional<Topic> getTopic(String id) {
return topicRepository.findById(id);
}
public void addTopic(Topic topic) {
topicRepository.save(topic);
}
public void updateTopic(String id, Topic topic) {
topicRepository.save(topic);
}
public void deleteTopic(String id) {
topicRepository.deleteById(id);
}
}
我能够在我的表中发布主题,并从邮差那里得到,但是当我试图使用put来更新表的内容时,没有任何变化。
我试图用下面的命令来更新表
{
"id": "javascript",
"name": "Update javascript",
"description": "Update javascript Description"
}
,"javascript"已经在表中,名称为javascript"description为"javascript description">
这是我的spring控制台日志
2021-03-03 17:33:05.625 INFO 9848 --- [ main]
com.shashank.CourseApiDataApplication : Starting
CourseApiDataApplication using Java 13.0.2 on DESKTOP-FGT8D3V with PID 9848
(D:MavenProjectscourse-api-datatargetclasses started by shash in
D:MavenProjectscourse-api-data)
2021-03-03 17:33:05.628 INFO 9848 --- [ main]
com.shashank.CourseApiDataApplication : No active profile set, falling
back to default profiles: default
2021-03-03 17:33:06.262 INFO 9848 --- [ main]
.s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA
repositories in DEFAULT mode.
2021-03-03 17:33:06.290 INFO 9848 --- [ main]
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository
scanning in 23 ms. Found 1 JPA repository interfaces.
2021-03-03 17:33:06.618 INFO 9848 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s):
8080 (http)
2021-03-03 17:33:06.623 INFO 9848 --- [ main]
o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-03 17:33:06.624 INFO 9848 --- [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache
Tomcat/9.0.43]
2021-03-03 17:33:06.720 INFO 9848 --- [ main] o.a.c.c.C.[Tomcat].
[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-03 17:33:06.720 INFO 9848 --- [ main]
w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext:
initialization completed in 1039 ms
2021-03-03 17:33:06.876 INFO 9848 --- [ main]
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing
PersistenceUnitInfo [name: default]
2021-03-03 17:33:06.901 INFO 9848 --- [ main]
org.hibernate.Version : HHH000412: Hibernate ORM core
version 5.4.28.Final
2021-03-03 17:33:06.964 INFO 9848 --- [ main]
o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons
Annotations {5.1.2.Final}
2021-03-03 17:33:07.016 INFO 9848 --- [ main]
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-03-03 17:33:07.018 WARN 9848 --- [ main]
com.zaxxer.hikari.util.DriverDataSource : Registered driver with
driverClassName=org.apache.derby.jdbc.EmbeddedDriver was not found, trying
direct instantiation.
2021-03-03 17:33:07.269 INFO 9848 --- [ main]
com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not
support get/set network timeout for connections. (Feature not implemented: No
details.)
2021-03-03 17:33:07.271 INFO 9848 --- [ main]
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-03-03 17:33:07.281 INFO 9848 --- [ main]
org.hibernate.dialect.Dialect : HHH000400: Using dialect:
org.hibernate.dialect.DerbyTenSevenDialect
2021-03-03 17:33:07.911 INFO 9848 --- [ main]
o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform
implementation:
[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-03-03 17:33:07.916 INFO 9848 --- [ main]
j.LocalContainerEntityManagerFactoryBean : Initialized JPA
EntityManagerFactory for persistence unit 'default'
2021-03-03 17:33:08.134 WARN 9848 --- [ main]
JpaBaseConfiguration$JpaWebConfiguration : 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
2021-03-03 17:33:08.220 INFO 9848 --- [ main]
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService
'applicationTaskExecutor'
2021-03-03 17:33:08.460 INFO 9848 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080
(http) with context path ''
2021-03-03 17:33:08.469 INFO 9848 --- [ main]
com.shashank.CourseApiDataApplication : Started CourseApiDataApplication
in 3.207 seconds (JVM running for 3.973)
你的Id有点不正常。为什么你认为我是字符串?应该是Long
或int
,所以也许你的id是你的主要问题。
另外,如果能把控制器放进去,可以帮助我们更好地调试。不管怎样,我还是有一些想法的。
public void updateTopic(String id, Topic topic) {
Topic myTopic = topicRepository.findById(id);
topicRepository.save(myTopic);
}
控制器@PutMapping("/topics")
private Topic update(@RequestBody Topic topic){
topicService.updateTopic(topic);
return topic;
}
- 强烈建议您将ID从字符串更改为
int
或Long
,并尝试上述解决方案。