无法将请求参数绑定到 spring mvc 中的对象?
unable to bind request parameters to the object in spring mvc?
您好,我有一个 angular ui,它消耗 spring 引导应用程序提供的剩余 api。从 angular ui 我正在 uing 进行 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
下面是我的名为CampaignResource的RestController&请求方法
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 class,我想将我的请求参数绑定到它。
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 进行调试,我可以看到状态和帐户都是空的。 !!!
感谢任何帮助
非常感谢。
您需要 setter 个集合方法作为集合对象而不是每个对象的基础。你有
public void addStatus(UUID status) {
this.statuses.add(status);
}
但是 spring 不知道如何设置 uuid,如果你为整个集合添加一个 setter 它会起作用,例如
public void setStatuses(List<UUID> statuses) {
this.statuses = statuses;
}
除此之外,我还建议您创建一个构造函数,其中包含您要设置的 class 的所有字段。这样你就不需要 setters 并且 class 将包含更少的样板文件。
您好,我有一个 angular ui,它消耗 spring 引导应用程序提供的剩余 api。从 angular ui 我正在 uing 进行 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
下面是我的名为CampaignResource的RestController&请求方法 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 class,我想将我的请求参数绑定到它。
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 进行调试,我可以看到状态和帐户都是空的。 !!!
感谢任何帮助 非常感谢。
您需要 setter 个集合方法作为集合对象而不是每个对象的基础。你有
public void addStatus(UUID status) {
this.statuses.add(status);
}
但是 spring 不知道如何设置 uuid,如果你为整个集合添加一个 setter 它会起作用,例如
public void setStatuses(List<UUID> statuses) {
this.statuses = statuses;
}
除此之外,我还建议您创建一个构造函数,其中包含您要设置的 class 的所有字段。这样你就不需要 setters 并且 class 将包含更少的样板文件。