Reputation: 1830
I have Stock response class with only two fields as follows
class StockResponse {
private String orderId;
private String status;
//constructor
//getters and setters
}
and the also the controller as follows
@RestController
@RequestMapping("/stocks")
public class StockController {
private static List<StockResponse> stocktList = new ArrayList <StockResponse > ();
static {
stocktList.add(new StockResponse("order1", "AVAILABLE"));
stocktList.add(new StockResponse("order2", "AVAILABLE"));
stocktList.add(new StockResponse("order3", "NOT AVAILABLE"));
stocktList.add(new StockResponse("order4", "AVAILABLE"));
}
@GetMapping("/")
public ResponseEntity < ? > getProsucts() {
return ResponseEntity.ok(stocktList);
}
@GetMapping(path="/{id}", produces = "application/json;charset=UTF-8")
public StockResponse getProsucts(@PathVariable String id) {
StockResponse product = findOrder(id);
if (product == null) {
// return ResponseEntity.badRequest(product)
// .body("Invalid product Id");
}
System.out.println(product.getOrderId());
System.out.println(product.getStatus());
return new StockResponse(product.getOrderId(), product.getStatus());
}
private StockResponse findOrder(String id) {
return stocktList.stream()
.filter(user -> user.getOrderId()
.equals(id))
.findFirst()
.orElse(null);
}
}
when I make a call to the localhost:8082/stocks/order1 I get a response with only one field showing up as follows
what could I be missing out?
Upvotes: 3
Views: 2966
Reputation: 1830
I ensured that all my getters and setters are public, I looked closely and I had missed public on
public String getStatus()
Upvotes: 1
Reputation: 2821
I am unable to reproduce this, meaning it works for me.
Listing all stocks
$ curl -sS 'http://localhost:8080/stocks/' | jq "."
[
{
"orderId": "order1",
"status": "AVAILABLE"
},
{
"orderId": "order2",
"status": "AVAILABLE"
},
{
"orderId": "order3",
"status": "NOT AVAILABLE"
},
{
"orderId": "order4",
"status": "AVAILABLE"
}
]
Getting specific stock
$ curl -sS 'http://localhost:8080/stocks/order1' | jq "."
{
"orderId": "order1",
"status": "AVAILABLE"
}
My StockController
is identical to yours. I also copy and pasted your StockResponse
to get the field names identical to yours, but since you didn't include the constructor/getter and setter I'll show the one which works for me.
The way you're instantiating the StockResponse
objects in the stocktList
list is using the constructor and that might point to that you're not actually setting the this.status
on the objects. If that doesn't work, double check that the getter for the status field is actually called getStatus()
.
public class StockResponse {
private String orderId;
private String status;
public StockResponse(String orderId, String status) {
this.orderId = orderId;
this.status = status;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
The fact that your response contains a field that uses "non-standard" upper case for the first letter tells me that maybe you're doing something else that's not standard the might influence your result.
Upvotes: 2