[英]How can I GET object only by 1 field under @RequestBody annotation?
我需要個建議。 我不太擅長Spring框架,我只想問一個簡單的問題。
我在服務器端有下一個POJO對象:
@Entity
@Table(name = "AutoRate")
public class AutoService {
public AutoService() {
}
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private long id;
@Column(name = "serviceName", nullable = false)
private String serviceName;
@Column(name = "imageURL", nullable = false)
private String imageURL;
@Column(name = "mapCoordinate", nullable = false)
private String mapCoordinate;
@Column(name = "websiteURL", nullable = false)
private String websiteURL;
@Column(name = "phoneNumber", nullable = false)
private String phoneNumber;
@JsonManagedReference
@OneToMany(mappedBy = "autoService", fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
private List<Service> services = new ArrayList<Service>();}
它包含所有獲取器和設置器; 我只想節省空間。
下一步:我想從數據庫中為客戶端獲取1個對象,我只想使用數據庫中的serviceName
變量來實現。
我有下一個控制器方法:
@RequestMapping(value = "/getAutoService", method = RequestMethod.GET)
@ResponseBody
public AutoService getAutoService(@RequestBody String serviceName){
AutoService autoService = dataBaseService.getByName(serviceName);
return autoService;
}
從客戶致電后,我收到下一個錯誤:
117899 [http-apr-8080-exec-1] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.webserverconfig.user.entity.AutoService com.webserverconfig.user.controller.ServiceController.getAutoService(com.webserverconfig.user.entity.AutoService)
我認為我使用@RequestBody
注釋錯誤。
有人可以給個建議嗎?
您正在使用GET方法,該方法在查詢字符串中而不是在HTTP正文中傳遞其數據,POST方法的確在其正文中傳遞HTTP消息。
因此,使用@RequestBody
批注是不正確的,請嘗試使用@PathVariable
。
@RequestBody
注釋表示您正在嘗試將HTTP正文消息中的數據綁定到method參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.