未解决原始关闭原因
以下问题:我正在为考试而学习,我想让这个程序正常工作。Spring Boot(用Spring Initializr得到了模板)使用REST web服务。程序正在启动和“工作”,只是不完全是我想要的方式。我想用Postman请求我的GetMapping和PostMapping(还没有在那里),但是我得到的是:
我尝试了所有在我脑海中闪过的东西,但我无法弄清楚。“相关帖子”也不适合我。Postman获得了连接,但它看不到任何数据,或者至少没有响应任何数据。
{
"timestamp": "2022-09-04T18:47:39.690+00:00",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/donation/"
}
我显然想得到实际的结果。我已经试着修了两个小时了,需要一些帮助。
Application.properties:
spring.datasource.url=jdbc:h2:mem:test
server.port=8080
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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>template</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>template</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用/主要:
package model;
import org.springframework.stereotype.Component;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
package main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class TemplateApplication {
public static void main(String[] args) {
SpringApplication.run(TemplateApplication.class, args);
}
}
个人或实体:
@Entity
@Table(name="Person")
public class Person implements Serializable {
@Id
@Column(name="person_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@NotNull
@Column(name="person_firstName")
private String firstName;
@NotNull
@Column(name="person_lastName")
private String lastName;
@OneToMany(mappedBy = "person")
private List<Donation> donations = new ArrayList<>();
public Person() {
}
public Person(String lastName, String firstName) {
this.firstName = firstName;
this.lastName = lastName;
this.donations = donations;
}
public Integer getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(id, person.id) && Objects.equals(firstName, person.firstName) && Objects.equals(lastName, person.lastName) && Objects.equals(donations, person.donations);
}
@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, donations);
}
@Override
public String toString() {
return "Donation{" +
"id=" + this.getId() +
", firstName='" + this.getFirstName() + '\'' +
", lastName='" + this.getLastName() + '\'' +
'}';
}
}
捐赠实体:
package model;
import org.springframework.stereotype.Component;
import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.PastOrPresent;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;
@Entity
@Table(name="Donation")
public class Donation implements Serializable {
@Id
@Column(name="donation_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Min(value=5)
@Column(name="donation_amount")
private Integer amount;
@Column(name="donation_date")
private LocalDate date;
@PastOrPresent
@ManyToOne
@JoinColumn(name="donation_person_id", nullable = false)
private Person person;
public Donation() {
}
public Donation(Person person,LocalDate date,Integer amount) {
this.amount = amount;
this.date = date;
this.person = person;
}
public Integer getId() {
return id;
}
public Integer getAmount() {
return amount;
}
public LocalDate getDate() {
return date;
}
public Person getPerson() {
return person;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Donation donation = (Donation) o;
return Objects.equals(id, donation.id) && Objects.equals(amount, donation.amount) && Objects.equals(date, donation.date) && Objects.equals(person, donation.person);
}
@Override
public int hashCode() {
return Objects.hash(id, amount, date, person);
}
@Override
public String toString() {
return "Person{" +
"id=" + this.getId() +
", amount=" + this.getAmount() +
", date=" + this.getDate() +
'}';
}
}
捐赠资料库:
package will.Repositories;
import will.model.Donation;
import will.model.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface DonationRepository extends JpaRepository<Donation,Integer> {
@Query("SELECT SUM(d.amount) FROM Donation d WHERE d.person = :person")
Integer sum (@Param("person") Person person);
}
人员存储库:
package will.Repositories;
import will.model.Donation;
import will.model.Person;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonRepository extends JpaRepository<Person,Integer> {
@Query("SELECT d FROM Donation d WHERE d.person = :person")
List<Donation> personsDonations (@Param("person") Optional<Person> person);
}
捐赠控制者:
package will.Controller;
import java.util.*;
import will.Repositories.DonationRepository;
import will.model.Donation;
import will.model.Person;
import will.Repositories.PersonRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/api/donation")
public class DonationController {
private final PersonRepository pRepository;
private final DonationRepository dRepository;
public DonationController(PersonRepository pRepository, DonationRepository dRepository) {
this.pRepository = pRepository;
this.dRepository = dRepository;
}
@GetMapping(path="")
public List<Donation> findAllDonations(){
return dRepository.findAll();
}
@GetMapping(value="{id}")
public Donation findDonationById (@PathVariable(value="id") Integer id){
Optional<Donation> donation = dRepository.findById(id);
if(!donation.isPresent()){
// throw new DonationNotFoundException("Eine Donation mit dieser id exisitiert nicht!");
}
return donation.get();
}
@GetMapping(value="?min={min}")
public List<Person> findMinSum (@PathVariable(value="min") Integer id){
List<Person> minSumPersonen;
List<Person> people = pRepository.findAll();
List<Person> richPeople= new ArrayList<>();
//
for(Person p : people){
if(dRepository.sum(p)>=id){
richPeople.add(p);
}
}
return richPeople;
//
//
//
// Map<Person,Integer> donationsSummen = new HashMap<>() {
// };
//
// for(Donation i : donations){
// if(donationsSummen.containsKey(i.getPerson())){
// donationsSummen.put(i.getPerson(),donationsSummen.get(i.getPerson())+i.getAmount());
// }
// else{
// donationsSummen.put(i.getPerson(),i.getAmount());
// }
// }
}
}
人员控制员:
package will.Controller;
import will.Repositories.DonationRepository;
import will.model.Donation;
import will.model.Person;
import will.Repositories.PersonRepository;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.validation.Valid;
@RestController
@RequestMapping("/api/person")
public class PersonController {
private PersonRepository pRepository;
private DonationRepository dRepository;
public PersonController(PersonRepository pRepository, DonationRepository dRepository) {
this.pRepository = pRepository;
this.dRepository = dRepository;
}
@GetMapping(value = "/{id}/donations")
List<Donation> personsDonations(@PathVariable(value = "id") Integer id) {
return pRepository.personsDonations(pRepository.findById(id));
}
@PostMapping(path = "/{personid}/donations")
public ResponseEntity<Donation> createDonation(@PathVariable Integer personid, @Valid
@RequestBody Donation donation) {
Optional<Person> person = pRepository.findById(personid);
// if (!emp.isPresent()) {
// throw new EmployeeNotFoundException("Employee not found: " + empid);
// }
//
// if (wh.getWhId() != null) {
// throw new WorkingHoursHasIdException("WH has id: " + wh.getWhId());
// }
donation.setPerson(person.get());
Donation savedDonation = dRepository.save(donation);
String path = "/{personid}/donations";
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().replacePath(path).build(savedDonation.getId());
return ResponseEntity.created(uri).body(savedDonation);
}
}
最后,在开始时初始化数据库的data.sql:
INSERT INTO Person (person_lastName, person_firstName) VALUES('Bauer', 'Barbara');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Bauer', 'Christoph');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Denk', 'Christian');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Hamberger', 'Claudia');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Hofer', 'Barbara');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Imbacher', 'Gustav');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Hofer', 'Andreas');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Langberger', 'Armin');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Maurer', 'Daniel');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Strasser', 'Julia');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Tomanek', 'Florian');
INSERT INTO Person (person_lastName, person_firstName) VALUES('Schuster', 'Elisabeth');
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(1, '2019-09-04', 25);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(2, '2019-09-04', 25);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(3, '2019-09-04', 25);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(4, '2019-09-04', 5);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(5, '2019-09-04', 35);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(6, '2019-09-04', 5);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(7, '2019-09-05', 22);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(8, '2019-09-04', 25);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(9, '2019-09-06', 25);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(10, '2019-09-11', 20);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(11, '2019-09-04', 25);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(12, '2019-09-04', 100);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(1, '2019-10-06', 15);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(5, '2019-10-07', 15);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(6, '2019-10-07', 15);
INSERT INTO Donation (donation_person_id, donation_date, donation_amount) VALUES(7, '2019-10-07', 15);
这是很多,但我真的需要一些帮助。可能是一个非常愚蠢的错误,我似乎找不到它。
我有一个习惯,即使控制器是默认的,我也总是使用/字符,我建议详细说明这个动作并尝试一下。
@RequestMapping("/")
您可以更改的另一件事是Spring中的默认上下文,您需要更改属性
server.servlet.context-path=/api
在这种情况下,您需要调用/api/donation
另一个细化是以下代码行:
@RestController
@Component
@RequestMapping("/person")
您不需要添加所有这些注释,@RestControlle在Spring Boot应用程序中创建Bean,注释@Component不是必需的。
我希望您能找到问题的解决方案,我将从验证API调用开始,然后是属性。
我在Spark上使用Python时遇到问题。我的应用程序有一些依赖项,如numpy、pandas、astropy等。我无法使用virtualenv创建具有所有依赖项的环境,因为集群上的节点除了HDFS之外没有任何公共挂载点或文件系统。因此,我一直坚持使用。我将站点包的内容打包在一个ZIP文件中,并使用
所以,我正在尝试为discord上的机器人编写代码,以便在google上搜索所需的图像。我能够让机器人用一个版本的代码(A1)响应简单的命令,只是有更多的选项,但是我无法让图像搜索代码(A2)工作,也无法将这两个代码组合在一起。如果能给我一些建议,告诉我哪里出了问题,我将不胜感激。:-)非常感谢。 (A1): const Discord=require('Discord.js');const cl
所以我不知道如何让桌子在雪花中旋转。我试图在这里为帮助页面建模https://docs.snowflake.com/en/sql-reference/constructs/pivot.html。 枢轴输出 sql文本:
其目的是对用户输入的分数进行排序、显示和平均。我还没弄明白。如果你已经解决了这个问题,请帮忙。到目前为止,我已经尝试了这段代码,但它不起作用。 我从这段代码中得到的只是一个编译器错误。有人有什么建议吗?谢谢你。
我在Android Studio做了一个登录和注册系统。每当我启动应用程序并登录或向数据库添加新用户时,都会出现以下错误: runtimeException:执行doInBackground()时发生错误 在这里输入代码02-04 16:51:58.079 123 63-12496/com.example.app e/AndroidRuntime:致命异常:AsyncTask#1 java.lan
问题内容: 我试图使用executemany将值插入数据库中,但对我来说不起作用。这是一个示例: 这给了我以下错误: 但是,当我更改列表时,它可以正常工作: 它按预期工作!我可以在数据库中看到数据。为什么第一个列表不起作用而第二个列表却不起作用? (PS:这只是一个示例,而不是实际的代码。为简单起见,我制作了一个小测试用例)。 问题答案: 根据我对执行力的了解,您的意思是, 或类似的东西。不要在s