Skip to content

Commit d105f84

Browse files
committed
barcode lib 2.7, update swagger doc
1 parent e444401 commit d105f84

File tree

5 files changed

+125
-33
lines changed

5 files changed

+125
-33
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
#libs
3-
barcode = "2.6.4"
3+
barcode = "2.7.0"
44
openapi-ui = "2.5.0"
55

66
#plugins

src/main/java/com/pnuema/java/barcode/barcodeapi/BarcodeApiApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@OpenAPIDefinition(servers = {
1212
@Server(url = "https://barcode.pnuema.com/", description = "Remote"),
1313
@Server(url = "https://barcodeapi.link/", description = "Remote"),
14-
@Server(url = "http://localhost:8443/", description = "Local")
14+
@Server(url = "http://localhost:8080/", description = "Local")
1515
})
1616
@SpringBootApplication
1717
@EnableCaching

src/main/java/com/pnuema/java/barcode/barcodeapi/BarcodeBody.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,75 @@
11
package com.pnuema.java.barcode.barcodeapi;
22

3+
import com.pnuema.java.barcode.EncodingType;
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
36
import java.util.Optional;
47

58
@SuppressWarnings("unused")
9+
@Schema(name = "BarcodeBody", requiredMode = Schema.RequiredMode.REQUIRED)
610
public class BarcodeBody {
11+
@Schema(
12+
description = "Symbology type",
13+
type = "String",
14+
example = "upca",
15+
implementation = EncodingType.class,
16+
requiredMode = Schema.RequiredMode.REQUIRED
17+
)
718
private String type;
19+
20+
@Schema(
21+
description = "Barcode data to be encoded",
22+
type = "string",
23+
example = "123456789012",
24+
requiredMode = Schema.RequiredMode.REQUIRED
25+
)
826
private String data;
27+
28+
@Schema(
29+
example = "png",
30+
allowableValues = { "png", "jpg", "gif" },
31+
defaultValue = "png",
32+
requiredMode = Schema.RequiredMode.NOT_REQUIRED
33+
)
934
private String imageFormat;
35+
36+
@Schema(
37+
description = "Desired image width",
38+
type = "int",
39+
example = "400",
40+
requiredMode = Schema.RequiredMode.NOT_REQUIRED
41+
)
1042
private Integer w;
43+
44+
@Schema(
45+
description = "Desired image height",
46+
type = "int",
47+
example = "200",
48+
requiredMode = Schema.RequiredMode.NOT_REQUIRED
49+
)
1150
private Integer h;
51+
52+
@Schema(
53+
allowableValues = { "false", "true" },
54+
defaultValue = "false",
55+
requiredMode = Schema.RequiredMode.NOT_REQUIRED
56+
)
1257
private boolean label;
58+
59+
@Schema(
60+
description = "Hex color code for bars",
61+
type = "string",
62+
example = "000000",
63+
requiredMode = Schema.RequiredMode.NOT_REQUIRED
64+
)
1365
private String barcolor;
66+
67+
@Schema(
68+
description = "Hex color code for spaces and background",
69+
type = "string",
70+
example = "ffffff",
71+
requiredMode = Schema.RequiredMode.NOT_REQUIRED
72+
)
1473
private String background;
1574

1675
public String getType() {

src/main/java/com/pnuema/java/barcode/barcodeapi/controllers/v1/AbstractV1Resource.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
import org.springframework.web.bind.annotation.RequestMapping;
44

55
@RequestMapping("/v1")
6-
public abstract class AbstractV1Resource {
7-
}
6+
public abstract class AbstractV1Resource {}

src/main/java/com/pnuema/java/barcode/barcodeapi/controllers/v1/BarcodeController.java

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.pnuema.java.barcode.Barcode;
44
import com.pnuema.java.barcode.EncodingType;
55
import com.pnuema.java.barcode.barcodeapi.BarcodeBody;
6+
import io.swagger.v3.oas.annotations.media.Schema;
67
import org.springframework.cache.annotation.Cacheable;
78
import org.springframework.http.HttpHeaders;
89
import org.springframework.http.MediaType;
@@ -22,36 +23,12 @@
2223
@RestController
2324
public class BarcodeController extends AbstractV1Resource {
2425

25-
@GetMapping(value = "/barcode/{type}/data/{data}")
26-
@Cacheable("barcodes")
27-
public ResponseEntity<byte[]> getBarcodeImage(
28-
@PathVariable(name = "type") String type,
29-
@PathVariable(name = "data") String data,
30-
@RequestParam(name = "imageFormat") Optional<String> imageFormat,
31-
@RequestParam(name = "w") Optional<Integer> width,
32-
@RequestParam(name = "h") Optional<Integer> height,
33-
@RequestParam(name = "label") Optional<Boolean> includeLabel,
34-
@RequestParam(name = "barcolor") Optional<String> barColor,
35-
@RequestParam(name = "background") Optional<String> background) throws IOException {
36-
37-
return generateBarcode(
38-
type,
39-
data,
40-
imageFormat,
41-
width,
42-
height,
43-
includeLabel,
44-
barColor,
45-
background
46-
);
47-
}
48-
4926
@PostMapping(value = "/barcode/",
50-
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
51-
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
27+
consumes = { MediaType.APPLICATION_JSON_VALUE },
28+
produces = { MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE }
29+
)
5230
@Cacheable("barcodes")
5331
public ResponseEntity<byte[]> getBarcodeImage(@RequestBody BarcodeBody body) throws IOException {
54-
5532
return generateBarcode(
5633
body.getType(),
5734
body.getData(),
@@ -64,6 +41,58 @@ public ResponseEntity<byte[]> getBarcodeImage(@RequestBody BarcodeBody body) thr
6441
);
6542
}
6643

44+
@GetMapping(
45+
name = "barcode",
46+
value = "/barcode/{type}/data/{data}",
47+
produces = { MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE }
48+
)
49+
@Cacheable("barcodes")
50+
public ResponseEntity<byte[]> getBarcodeImage(
51+
@Schema(example = "upca", implementation = EncodingType.class, defaultValue = "upca")
52+
@PathVariable(name = "type")
53+
String type,
54+
55+
@Schema(example = "123456789012")
56+
@PathVariable(name = "data")
57+
String data,
58+
59+
@Schema(example = "png", allowableValues = { "png", "jpg", "gif" }, defaultValue = "png")
60+
@RequestParam(name = "imageFormat")
61+
Optional<String> imageFormat,
62+
63+
@Schema(example = "400")
64+
@RequestParam(name = "w")
65+
Optional<Integer> width,
66+
67+
@Schema(example = "200")
68+
@RequestParam(name = "h")
69+
Optional<Integer> height,
70+
71+
@Schema(allowableValues = { "false", "true" }, defaultValue = "false")
72+
@RequestParam(name = "label", defaultValue = "false")
73+
Optional<Boolean> includeLabel,
74+
75+
@Schema(example = "000000")
76+
@RequestParam(name = "barcolor")
77+
Optional<String> barColor,
78+
79+
@Schema(example = "ffffff")
80+
@RequestParam(name = "background")
81+
Optional<String> background
82+
83+
) throws IOException {
84+
return generateBarcode(
85+
type,
86+
data,
87+
imageFormat,
88+
width,
89+
height,
90+
includeLabel,
91+
barColor,
92+
background
93+
);
94+
}
95+
6796
private ResponseEntity<byte[]> generateBarcode(
6897
String type,
6998
String data,
@@ -72,7 +101,8 @@ private ResponseEntity<byte[]> generateBarcode(
72101
Optional<Integer> height,
73102
Optional<Boolean> includeLabel,
74103
Optional<String> barColor,
75-
Optional<String> background) throws IOException {
104+
Optional<String> background
105+
) throws IOException {
76106

77107
Barcode barcode = new Barcode();
78108

@@ -119,7 +149,11 @@ private ResponseEntity<byte[]> generateBarcode(
119149
//attach debug info to header
120150
responseHeaders.set("x-barcode-version", barcode.getTitle() + " " + barcode.getVersion());
121151
responseHeaders.set("x-raw-value", barcode.getRawData());
122-
responseHeaders.set("x-label-font", barcode.getLabelFont().getName());
152+
153+
if (barcode.isIncludeLabel()) {
154+
responseHeaders.set("x-label-font", barcode.getLabelFont().getName());
155+
}
156+
123157
responseHeaders.set("x-served-by", getMachineName());
124158

125159
if (exception != null || image == null) {

0 commit comments

Comments
 (0)