Reputation: 19
I'm creating a program that emulates a store front. I'm trying to test the search function that returns the vendor name after I input a name. I'm using swagger UI to input a parameter that returns an expected response.
Here the link to source code: https://github.com/opensource-io/training_storefront
I'm using this entity. I've trimmed the getters and setters as they aren't relevant.
@Entity
@Component
@Scope("prototype")
@Table(name="VENDORS", schema = "TRAINING_STOREFRONT")
public class VendorEntity {
@Id
@Column(name="VENDOR_KEY")
@NotNull
private Integer vendorKey;
@Column(name="VENDOR_NAME")
@NotNull
private String vendorName;
. . .
I'm using Spring Data REST to create the RESTful repositories. Here is my Spring Data REST JPA interface.
@RepositoryRestResource(collectionResourceRel = "vendors", path = "vendors")
public interface VendorRepository extends JpaRepository<VendorEntity, Integer> {
Set<VendorEntity> findByVendorNameContainsIgnoreCase(@Param("vendorName") final String vendorName);
List<VendorEntity> findAllByOrderByVendorKeyAsc();
}
I'm using Swagger to document and test my API. When I use Swagger or curl to send the parameter:
{ "vendorName" : "Red Hat" }
Curl
curl -X GET --header 'Content-Type: application/json' --header 'Accept: application/hal+json' -d 'red hat' 'http://localhost:8080/vendors/search/getVendorByVendorNameContainsIgnoreCase
Here is what is returned:
{
"cause": {
"cause": null,
"message": "Value must not be null!"
},
"message": "Value must not be null!; nested exception is java.lang.IllegalArgumentException: Value must not be null!"
}
This is whats expected:
{
"content": [
[
{
"vendorKey": 0,
"vendorName": "string"
}
]
],
"links": [
{
"href": "string",
"rel": "string",
"templated": true
}
]
}
Upvotes: 1
Views: 752
Reputation: 484
As Alan has pointed out in comments, when calling a search resource in Spring Data REST, you should provide the search parameter as GET parameter directly in the URL.
It is not directly stated in the documentation, but you can see it e.g. here.
Your REST call is returning this error, because you have not provided a parameter.
Correct CURL call would be formed like this:
curl -X GET --header 'Content-Type: application/json' --header 'Accept: application/hal+json' 'http://localhost:8080/vendors/search/getVendorByVendorNameContainsIgnoreCase?vendorName=red%20hat'
Swagger unfortunatelly does not cooperate with Spring Data REST so well, so sometimes in generates confusing examples.
Upvotes: 1