0

I used spring boot and post man in my project.In controller, i used method get all category look like :

@GetMapping(value = "/category")
@ResponseStatus(HttpStatus.OK)
public List<ReadableCategory> findAllCategories(@RequestParam(value = "filter", required = false) List<String> filter, MerchantStore store, Language language) {
    return categoryService.findCategories(store, 0, language, filter);
}

MerchanStore:

public class MerchantStore implements Auditable {

private final static String DEFAULT_STORE = "DEFAULT";

@Id
@Column(name = "MERCHANT_ID", unique = true, nullable = false)
@TableGenerator(name = "TABLE_GEN", table = "SM_SEQUENCER", pkColumnName = "SEQ_NAME",
        valueColumnName = "SEQ_COUNT", pkColumnValue = "STORE_SEQ_NEXT_VAL")
@GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
@JsonProperty(

        "merchant_id")
private Integer id;

@Column(name = "STORE_NAME", nullable = true, length = 100)
@JsonProperty(value = "store_name")
private String storeName;

@Pattern(regexp = "^[a-zA-Z0-9_]*$")
@Column(name = "STORE_CODE", nullable = true, unique = true, length = 100)
@JsonProperty(value = "merchant_id")
private String code;

@Column(name = "STORE_PHONE", length = 50)
private String storePhone;

@Column(name = "STORE_ADDRESS")
private String storeAddress;

@Column(name = "STORE_CITY", length = 100)
private String storeCity;

@Column(name = "STORE_POSTAL_CODE", length = 15)
private String storePostcode;

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Country.class)
@JoinColumn(name = "COUNTRY_ID", nullable = true)
private Country country;

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Zone.class)
@JoinColumn(name = "ZONE_ID", nullable = true)
private Zone zone;

@Column(name = "STORE_STATE_PROV", length = 100)
private String storeStateProvince;

@Column(name = "WEIGHTUNITCODE", length = 5)
private String wightUnitCode = MeasureUnit.LB.name();

@Column(name = "SEIZEUNITCODE", length = 5)
private String seizeUnitCode = MeasureUnit.IN.name();

@Column(name = "IN_BUSINESS_SINCE")
private LocalDate inBusinessSince = LocalDate.now();

@Transient
private String dateBusinessSince;

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Language.class)
@JoinColumn(name = "LANGUAGE_ID", nullable = false)
private Language defaultLanguage;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "MERCHANT_LANGUAGE")
private List<Language> languages = new ArrayList<>();

//some thing fields and get/set methods

Language.class

@Id
@Column(name = "LANGUAGE_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Embedded
private AuditSection auditSection = new AuditSection();

@Column(name = "CODE", nullable = false)
private String code;

@Column(name = "SORT_ORDER")
private Integer sortOrder;

@OneToMany(mappedBy = "defaultLanguage",targetEntity = MerchantStore.class)
@ToString.Exclude
@JsonIgnoreProperties
private List<MerchantStore> storesDefaultLanguage;

@ManyToMany(mappedBy = "languages",targetEntity = MerchantStore.class,fetch = FetchType.EAGER)
@ToString.Exclude
@JsonIgnoreProperties
private List<MerchantStore> stores = new ArrayList<>();

public String getCode() {
    return code;
}

When i request from postman looklike: http://localhost:8585/api/v1/id=1&storeName=2&code=3&languages=1,2,3

It work but because my class MerchanStore have a lot fields when i request all fields. Example : 10 field , my url very long and complex. I have two question:

  • Is there a better way to post the param without putting it on the url using GET request(not Post Request).

  • When i put id=1, because class MerchanStore contain id and language also contain id, it put same value 1. How to specific value
    MerchanStore with Id and Language with Id.

trungtbdn11
  • 21
  • 1
  • 3

2 Answers2

0

Is there a better way to post the param without putting it on the url using GET request(not Post Request).

No. Using GET with request parameters indicates your URL can be bookmarked. Especially for searching or filtering purpose, it is very convenient and useful.

When i put id=1, because class MerchanStore contain id and language also contain id, it put same value 1. How to specific value MerchanStore with Id and Language with Id.

I have no idea for this, maybe you can post it as a new question.

LHCHIN
  • 3,679
  • 2
  • 16
  • 34
  • thank you. But as i write in my post, my object a lot fields. When i put all in url, it very long. When i know, url limit size url ? Is best way do it ? Post something to header or some thing like this – trungtbdn11 Oct 30 '19 at 03:38
  • @trungtbdn11 Yes, URL has maximum length, you can refer to [What is the maximum length of a URL in different browsers?](https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers). But most popular web APs also did this such as JIRA, they even use JQL to achieve advanced search with GET method. – LHCHIN Oct 30 '19 at 03:43
0

You can use @ModelAttribute.. Try with this:

@GetMapping(value = "/category")
@ResponseStatus(HttpStatus.OK)
public List<ReadableCategory> findAllCategories(@RequestParam(value = "filter", required = false) List<String> filter, @ModelAttribute MerchantStore store, @ModelAttribute Language language) {
    return categoryService.findCategories(store, 0, language, filter);
}
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39