I work with a small Spring app where I have few values in the database and I would like to retrieve them using the mutable call.
The API is here,
@RestController
@RequestMapping("/api/v1/products")
public class ProductAPI {
private ProductService service;
@Autowired
public void setService(ProductService service) {
this.service = service;
}
@GetMapping("/stock/")
public ResponseEntity<Product> findById(@RequestParam("productId") String productId) {
Product product = service.findById(productId).get();
return ResponseEntity.of(Optional.of(product));
}
...........
}
The service call,
@Service
public class ProductService {
private ProductRepository repository;
@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.repository = productRepository;
}
public Optional<Product> findById(String id) {
return repository.findById(id);
}
}
The repository class,
@Repository
public interface ProductRepository extends CrudRepository<Product, String>{
}
When I make the call using the cURL, I get the message,
$ curl -X GET http://localhost:8080/api/v1/products/stock?productId=Product%20ID | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 142 0 142 0 0 845 0 --:--:-- --:--:-- --:--:-- 850
{
"timestamp": "2019-02-25T12:19:31.797+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/v1/products/stock"
}
I have the entries in the database inserted correctly. What is the issue here?
Because you have extra / in your mapping
@GetMapping("/stock/")
so if you want request like this
curl -X GET http://localhost:8080/api/v1/products/stock/?productId=Product%20ID
you need mapping like:
@GetMapping("/stock")
in you current version right curl looks like:
http://localhost:8080/api/v1/products/stock/?productId=Product%20ID
Since you have clearly mentioned the mapping as @GetMapping("/stock/") in your controller, and when you are trying to access the resource by the path /stock obviously there is no such mapping. Due to that, you are getting 404 found exception.
So, update the mapping like @GetMapping("/stock").
Happy Learning!
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.