@dryv/spring-boot-backendv0.1.0
Spring MVC controllers with shared Jackson models and separated service boundaries.
dryv pack add @dryv/spring-boot-backendpackage {{ pack.inputs.java_package }};
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.util.MultiValueMap;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@Component
public final class DryvSpringBinding {
private final ObjectMapper objectMapper;
public DryvSpringBinding(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public <T> T decode(String raw, Class<T> type) {
if (raw == null) {
return null;
}
try {
return objectMapper.convertValue(raw, type);
} catch (IllegalArgumentException error) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid request value", error);
}
}
public <T> T decode(JsonNode raw, Class<T> type) {
if (raw == null || raw.isNull()) {
return null;
}
return objectMapper.convertValue(raw, type);
}
public <T> List<T> decodeMany(List<String> values, Class<T> type) {
if (values == null) {
return Collections.emptyList();
}
List<T> result = new ArrayList<>(values.size());
for (String value : values) {
result.add(decode(value, type));
}
return result;
}
public <T> List<T> decodeMany(JsonNode values, Class<T> type) {
if (values == null || values.isNull()) {
return null;
}
if (!values.isArray()) {
return List.of(decode(values, type));
}
List<T> result = new ArrayList<>(values.size());
for (JsonNode value : values) {
result.add(decode(value, type));
}
return result;
}
public JsonNode requiredBody(JsonNode body) {
if (body == null || body.isNull()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "request body is required");
}
return body;
}
public JsonNode requiredBody(JsonNode body, String name) {
if (body == null || !body.has(name)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing required body field " + name);
}
return body.get(name);
}
public List<String> requiredHeaders(HttpHeaders headers, String name) {
List<String> values = headers.get(name);
if (values == null || values.isEmpty()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing required header " + name);
}
return values;
}
public String requiredParam(MultiValueMap<String, String> params, String name) {
String value = params.getFirst(name);
if (value == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing required parameter " + name);
}
return value;
}
public List<String> requiredParams(MultiValueMap<String, String> params, String name) {
List<String> values = params.get(name);
if (values == null || values.isEmpty()) {
throw new IllegalArgumentException("missing required parameter " + name);
}
return values;
}
public String requiredPath(Map<String, String> path, String name) {
String value = path.get(name);
if (value == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing required path parameter " + name);
}
return value;
}
public String requiredHeader(HttpHeaders headers, String name) {
String value = headers.getFirst(name);
if (value == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing required header " + name);
}
return value;
}
public String cookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return null;
}
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name)) {
return cookie.getValue();
}
}
return null;
}
public String requiredCookie(HttpServletRequest request, String name) {
String value = cookie(request, name);
if (value == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "missing required cookie " + name);
}
return value;
}
}