I am creating a rest service and providing json to our consumer. I have two fields cardType, cardDetails ( i have more fields but these are what relevant).
If cardType is credit card then only I want to send another field cardDetails in json.
If cardtype is something else, Then I don't want to send card details(not even empty data) . I am not getting how can i achieve solution for this.
Which language do you use? In spring framework you can do it very easy. You can add if condition in your controller.
@RestController
public class YourController {
@Autowired
YourRepository yourRepository;
@RequestMapping("/creaditCardsInfo")
public String sendCreditCardToCustomer(@RequestParam(value = "creditCardType", defaultValue = "none") String cardType,@RequestParam(value = "cardDetails", default = "someInfo") String cardDetails) {
if("credit".equals(cardType.trim()) && !(cardDetails.isEmpty())){
return new RequredCreditInfoObject();
}
if you use maven your pom.xml should contain required libs
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JPA Data (if We are going to use Repositories, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<!-- for JSon marshaling -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
Good luck,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.