Commit aff1fefa by Patryk Czarnik

SpringJersey: Pozostałe elementy implementacji

parent af868a09
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Customer;
public interface CustomerRepository extends JpaRepository<Customer, String> {
}
package sklep.rest;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
@Provider
public class DefaultExceptionMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception e) {
String tresc = String.format("<html><body>"
+ "<h1>Błąd</h1>"
+ "<div>Błąd typu <code>%s</code></div>"
+ "<div style='color:red'>%s</div>"
+ "</body></html>",
e.getClass().getName(),
e.getMessage());
return Response.status(Status.INTERNAL_SERVER_ERROR)
.type("text/html;charset=utf-8")
.entity(tresc)
.build();
}
}
package sklep.rest;
import java.util.List;
import java.util.Optional;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriBuilder;
import sklep.model.Customer;
import sklep.repository.CustomerRepository;
@Path("/customers")
@Produces({ "application/xml", "application/json", "text/plain"})
@Consumes({ "application/xml", "application/json" })
public class RCustomers {
private CustomerRepository customerRepository;
@POST
public Response create(final Customer customer) {
customerRepository.save(customer);
return Response.created(UriBuilder.fromResource(RCustomers.class).path(customer.getCustomerEmail()).build()).build();
}
@GET
@Path("/{email}")
public Response findByEmail(@PathParam("email") final String email) {
Optional<Customer> customer = customerRepository.findById(email);
if(customer.isPresent()) {
return Response.ok(customer.get()).build();
} else {
return Response.status(Status.NOT_FOUND).build();
}
}
@GET
public List<Customer> listAll() {
return customerRepository.findAll();
}
// public List<Customer> listAll(@QueryParam("start") final Integer startPosition,
// @QueryParam("max") final Integer maxResult) { customerRepository.findAll(Pageable.ofSize(max).....) }
@DELETE
@Path("/{email}")
public Response deleteById(@PathParam("email") final String email) {
customerRepository.deleteById(email);
return Response.noContent().build();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment