我花了整个周末试图调试这段代码。我有一个Spring RestController:
import com.tsakirogf.schedu.model.ContactMean;
import com.tsakirogf.schedu.model.DefaultContactMean;
import com.tsakirogf.schedu.model.human.Business;
import com.tsakirogf.schedu.services.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
import java.util.Set;
@RestController
@RequestMapping("api/v1/business/")
public class BusinessController
{
@Autowired
BusinessService businessService;
@GetMapping(value = "businesss")
Iterable<Business> list()
{
Iterable<Business> retVal = businessService.findAll();
return retVal;
}
@RequestMapping(value = "business", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
Business create(@RequestBody Business business)
{
CollectionOfContactMethods collectionOfContact = business.getContact();
collectionOfContact.setBusiness(business);
Set<ContactMean> contactMeanSet = collectionOfContact.getContactMeans();
DefaultContactMean defaultContactMeanSet = collectionOfContact.getDefaultContactMean();
defaultContactMeanSet.getCollectionOfContactMethodsDefault().setId(collectionOfContact.getId());
for (ContactMean element : contactMeanSet)
{
element.setCollectionOfContactMethods(collectionOfContact);
}
collectionOfContact.setDefaultContactMean(defaultContactMeanSet);
business.setContact(collectionOfContact);
Business retval = businessService.save(business);
return retval;
}
@RequestMapping(value = "business/{id}", method = RequestMethod.GET )
Optional<Business> get(@PathVariable Long id)
{
return businessService.findById(id);
}
}
而且这项服务:
public interface BusinessService extends CrudRepository<Business, Long>
{
}
这是模型:
@Table(name = "business")
public class Business
{
@Id
@Column(name = "business_id", nullable = false)
private Long id;
@JsonProperty("name")
private String name;
@Embedded
@JsonProperty("address")
private Address address;
@OneToMany(mappedBy = "business",
cascade = CascadeType.ALL,
fetch = FetchType.LAZY)
@JsonProperty("operatives")
@JsonIgnore
Set<Professional> operatives;
@OneToOne(mappedBy = "business",
cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
optional = false)
@JsonBackReference
@JsonProperty("contact_numbers")
private CollectionOfContactMethods contact;
public Business()
{
}
// Getters and Setters
}
当我发送这样的POST请求:
我从哪里获取以下内容
{ "timestamp": "2021-11-01T08:59:06.343+00:00", "status": 500, "error": "Internal Server Error", "path": "/api/v1/business/business" }
我调试过程中遇到了 InvocationTargetException 错误,如下所示:
这是控制器,在 save() 函数之前,似乎抛出了异常:
以下为异常捕获代码:
![enter image description here](https://istack.dev59.com/hyitD.webp)
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.hbm2ddl.auto=create
我希望能得到任何有关编程的想法。感谢您抽出时间。
id
字段,它没有分配生成器。因此,在保存对象之前,您需要更改该字段或手动分配一个id
。 - M. Deinum