嗨,我有一个Angular ui,它消耗了spring boot应用程序提供的rest api。从角度ui,我正在发出GET rest api调用,但是请求参数未绑定到该对象。以下是我的GET请求。
curl -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJjYW1wYWlnbm1hbmFnZXJAbG9jYWxob3N0IiwiYXV0aCI6IlJPTEVfQ0FNUEFJR05fTUFOQUdFUiIsImV4cCI6MTU1ODE4MzAyM30.OHSqVZ5c9-44SyyB_ykFqf9xC-06UvSv-F7UYLvrrK_YNJrqF3Mvuv8zvTrBqdMXRMBdCQNmitVQ38zdZxj3Tg" http://localhost:8080/api/campaigns/unpaginated?statuses=357632f0-1afd-4af2-a8f2-3b964884bfb3&statuses=2f02e5f0-2d56-4583-a9db-f962becbd5f9&accounts=e15965cf-ffc1-40ae-94c4-b450ab190222
以下是我的RestController命名为CampaignResource和request方法 getAllCampaignsUnpaginated
@RestController
@RequestMapping("/api")
public class CampaignResource {
/**
* GET /campaigns : get all the campaigns unpaginated.
*
* @return the ResponseEntity with status 200 (OK) and the list of campaigns in body
*/
@GetMapping("/campaigns/unpaginated")
@Timed
@Secured({AuthoritiesConstants.GLOBAL_ADMIN, AuthoritiesConstants.ACCOUNT_ADMIN, AuthoritiesConstants.CAMPAIGN_MANAGER, AuthoritiesConstants.TEAM_MEMBER})
public ResponseEntity<List<DropdownDTO>> getAllCampaignsUnpaginated(CampaignFilterRequest filter) {
log.debug("REST request to get all Campaigns");
return ResponseEntity.ok().body(campaignService.findAll(filter));
}
}
以下是我想将我的请求参数绑定到的CampaignFilterRequest类。
import com.google.common.collect.Lists;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
public class CampaignFilterRequest {
private ZonedDateTime minStartDate;
private ZonedDateTime maxEndDate;
private List<UUID> types = Lists.newArrayList();
private List<UUID> createdBy = Lists.newArrayList();
private List<UUID> statuses = Lists.newArrayList();
private List<UUID> accounts = Lists.newArrayList();
public ZonedDateTime getMinStartDate() {
return minStartDate;
}
public void setMinStartDate(ZonedDateTime minStartDate) {
this.minStartDate = minStartDate;
}
public ZonedDateTime getMaxEndDate() {
return maxEndDate;
}
public void setMaxEndDate(ZonedDateTime maxEndDate) {
this.maxEndDate = maxEndDate;
}
public List<UUID> getStatuses() {
return statuses;
}
public void addStatus(UUID status) {
this.statuses.add(status);
}
public List<UUID> getTypes() {
return types;
}
public void setTypes(List<UUID> types) {
this.types = types;
}
public void addType(UUID type) {
this.types.add(type);
}
public List<UUID> getCreatedBy() {
return createdBy;
}
public void setCreatedBy(List<UUID> createdBy) {
this.createdBy = createdBy;
}
public void addCreatedBy(UUID createdBy) {
this.createdBy.add(createdBy);
}
public List<UUID> getAccounts() {
return accounts;
}
public void addAccount(UUID accounts) {
this.accounts.add(accounts);
}
public void setAccounts(List<UUID> accounts) {
this.accounts = accounts;
}
}
我可以对getAllCampaignsUnpaginated进行调试,并且可以看到状态和帐户为空。 !!!
感谢任何帮助 非常感谢。
答案 0 :(得分:0)
您需要将集合的setter方法作为集合对象而不是每个对象。你有
public void addStatus(UUID status) {
this.statuses.add(status);
}
但是spring不知道如何设置uuid,例如,如果您为整个集合添加一个setter,它将起作用,例如
public void setStatuses(List<UUID> statuses) {
this.statuses = statuses;
}
此外,我还建议您创建一个构造函数,其中包含要设置的类的所有字段。这样,您就不需要二传手了,并且该类将包含更少的样板。