Commit c336c76c by Patryk Czarnik

Ostatnie klasy w przykładzie Jersey

parent b3f43b8b
...@@ -20,6 +20,7 @@ dependencies { ...@@ -20,6 +20,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jersey' implementation 'org.springframework.boot:spring-boot-starter-jersey'
runtimeOnly 'org.postgresql:postgresql' runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.glassfish.jaxb:jaxb-runtime'
} }
tasks.named('bootBuildImage') { tasks.named('bootBuildImage') {
......
package sklep.photo;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
@Component
public class PhotoUtil {
@Value("${alx.photo_dir}")
private String photoDir;
private static final String EXT = ".jpg";
public File getFile(int productId) {
Path path = getPath(productId);
File file = path.toFile();
if(file.exists()) {
return file;
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot read photo for product id = " + productId);
}
}
public byte[] readBytes(int productId) {
Path path = getPath(productId);
try {
return Files.readAllBytes(path);
} catch (IOException e) {
// System.err.println(e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot read photo for product id = " + productId);
}
}
public void writeStream(int productId, InputStream inputStream) {
try {
Path path = getPath(productId);
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
// wypisujemy błąd, ale metoda kończy się normalnie
e.printStackTrace();
}
}
public void writeBytes(int productId, byte[] bytes) {
try {
Path path = getPath(productId);
Files.write(path, bytes);
} catch (Exception e) {
e.printStackTrace();
}
}
private Path getPath(int productId) {
String fileName = productId + EXT;
return Paths.get(photoDir, fileName);
}
}
...@@ -13,6 +13,8 @@ public class JerseyConfig extends ResourceConfig { ...@@ -13,6 +13,8 @@ public class JerseyConfig extends ResourceConfig {
public JerseyConfig() { public JerseyConfig() {
register(RHello.class); register(RHello.class);
register(RTime.class); register(RTime.class);
register(RProducts.class);
register(ROrder.class);
} }
} }
package sklep.rest;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import sklep.model.Order;
import sklep.repository.OrderRepository;
@Path("/orders")
@Produces("application/json")
@Consumes("application/json")
public class ROrder {
@Autowired
private OrderRepository orderRepository;
@GET
public List<Order> readAll() {
return orderRepository.findAll();
}
@Path("/{id}")
@GET
public Order readOne(@PathParam("id") int id) {
Optional<Order> order = orderRepository.findById(id);
if(order.isPresent()) {
return order.get();
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Nie ma zamówienia o numerze " + id);
}
}
}
package sklep.rest;
import java.net.URI;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
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.UriBuilder;
import sklep.model.Product;
import sklep.photo.PhotoUtil;
import sklep.repository.ProductRepository;
@Path("/products")
public class RProducts {
@Autowired
private ProductRepository productRepository;
@Autowired
private PhotoUtil photoUtil;
@GET
@Produces({"application/json", "application/xml", "text/plain"})
public List<Product> getAllProducts() {
return productRepository.findAll();
}
@GET
@Path("/{id}")
@Produces({"application/json", "application/xml", "text/plain"})
public Product readOne(@PathParam("id") int productId) {
return productRepository
.findById(productId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND)); }
@POST
@Consumes({"application/json", "application/xml"})
public Response saveProduct(Product product) {
productRepository.save(product);
URI uri = UriBuilder
.fromResource(RProducts.class)
.path(String.valueOf(product.getProductId()))
.build();
return Response.created(uri).build();
}
@DELETE
@Path("/{id}")
public void delete(@PathParam("id") int productId) {
productRepository.deleteById(productId);
}
@GET
@Path("/{id}/photo")
@Produces("image/jpeg")
public byte[] getPhoto(@PathParam("id") int productId) {
return photoUtil.readBytes(productId);
}
@PUT
@Path("/{id}/photo")
@Consumes("image/jpeg")
public void uploadPhoto(@PathParam("id") int productId, byte[] bytes) {
photoUtil.writeBytes(productId, bytes);
}
}
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