0

I am trying to make an e-commerce mobile application and the backend for that is built in Spring boot with JPA. now I need to send a delete request to my server from the app. I can't use the primary key for that purpose. I need to delete them by name or email.

the normal delete URL for delete request will look something like this

https:localhost:8080/api/customers/1 

but in my case, I need to delete the customers based on the email not by id my Jpa code looks like this

public interface CustomerRepository extends JpaRepository<Customer, Integer> {

@Transactional
@RestResource(exported = true)
List<Customer> deleteByEmail(String email);

// the url will look something like this: http://localhost:8080/api/customers/search/findByEmail?customer_email=thekopsfc.sd@gmail.com
Customer findByEmail(@Param("customer_email") String email);

}

how could I send a delete request to the server?

javaNoob
  • 86
  • 1
  • 9
  • Can you find customer by email? – MxWild Dec 27 '19 at 12:48
  • @MxWild yes... i used same for delete but it doesn't seem to work.. i would gladly provide additional code if you can help.. the request url looked like this http://localhost:8080/api/customer/search/findByEmail?email=helloworld@gmail.com – javaNoob Dec 27 '19 at 12:51
  • Can you share the code for findByEmail? And how you make call to it? – miyav miyav Dec 27 '19 at 12:55
  • @miyavmiyav i have edited the question please do check – javaNoob Dec 27 '19 at 12:59
  • On the backed you does have a controller with @DeleteMapping("customers/{email}") {service.deleteByEmail(email);} And than you can get this url. – MxWild Dec 27 '19 at 13:01
  • See your findByEmail methodhas \@Param annotation so that it checks for value after ?customer_email= Other way to use it you can use path params like the comment above here are some examples: https://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam Also another way is \@RequestBody but DELETE doesnt use that your delete method doesnt provide anything like that unless I'm not completelty sure what \@RestResource annotation does (not using springboot that much – miyav miyav Dec 27 '19 at 13:06

1 Answers1

0

check this building-user-authentication-android using retrofit2.

In you controller:

@Autowired
private CustomerRepository customerRepository;

@RequestMapping(value = "/api/customers/delete", method = RequestMethod.POST)
    public List<Customer> deleteByEmail(@RequestBody String email){
        return customerRepository.deleteByEmail(email);
    }

In your android apllication:

build.gradle

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

add Network.java:

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Network {

    private static HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

    public static Retrofit getInstance() {
        Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://192.168.43.126:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build());
        return builder.build();

    }
}

your interface ApiService.java

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;

public interface ApiService {

    @POST("//api/customers/delete")
    Call<Customer> deleteByEmail(@Body Customer customer);

}