Skip to content

Commit a64e55e

Browse files
committed
Add Generic Type Erasure for List<> return types
1 parent 6941ead commit a64e55e

File tree

5 files changed

+192
-64
lines changed

5 files changed

+192
-64
lines changed

jax-rs-context/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@
7676
<extractDir>c:/tmp/servers</extractDir>
7777
</zipUrlInstaller>
7878
</container>
79-
79+
<!--<container>-->
80+
<!--<containerId>glassfish4x</containerId>-->
81+
<!--<zipUrlInstaller>-->
82+
<!--<url>http://download.java.net/glassfish/4.1/release/glassfish-4.1.zip</url>-->
83+
<!--</zipUrlInstaller>-->
84+
<!--</container>-->
8085
<deployables>
8186
<deployable>
8287
<groupId>${project.groupId}</groupId>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.readlearncode.restserver;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
/**
6+
* Source code github.com/readlearncode
7+
*
8+
* @author Alex Theedom www.readlearncode.com
9+
* @version 1.0
10+
*/
11+
@XmlRootElement
12+
public class Book {
13+
14+
private String isbn;
15+
private String title;
16+
private String author;
17+
private Float price;
18+
19+
public Book() {
20+
}
21+
22+
public Book(String isbn, String title, String author, Float price){
23+
this.isbn = isbn;
24+
this.title = title;
25+
this.author = author;
26+
this.price = price;
27+
}
28+
29+
public Book(String isbn) {
30+
this.isbn = isbn;
31+
}
32+
33+
public Book(String title, String author, Float price) {
34+
this.title = title;
35+
this.author = author;
36+
this.price = price;
37+
}
38+
39+
public String getIsbn() {
40+
return isbn;
41+
}
42+
43+
public void setIsbn(String isbn) {
44+
this.isbn = isbn;
45+
}
46+
47+
public String getTitle() {
48+
return title;
49+
}
50+
51+
public void setTitle(String title) {
52+
this.title = title;
53+
}
54+
55+
public String getAuthor() {
56+
return author;
57+
}
58+
59+
public void setAuthor(String author) {
60+
this.author = author;
61+
}
62+
63+
public Float getPrice() {
64+
return price;
65+
}
66+
67+
public void setPrice(Float price) {
68+
this.price = price;
69+
}
70+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.readlearncode.restserver;
2+
3+
import javax.annotation.PostConstruct;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Source code github.com/readlearncode
9+
*
10+
* @author Alex Theedom www.readlearncode.com
11+
* @version 1.0
12+
*/
13+
14+
public class BookRepository {
15+
16+
@PostConstruct
17+
public void initiate(){
18+
books.add(new Book("Java Fun","Alex Theedom", 10f));
19+
books.add(new Book("Java 101","Alex Theedom", 10f));
20+
books.add(new Book("Java Expert","Alex Theedom", 10f));
21+
books.add(new Book("Java EE 8","Alex Theedom", 10f));
22+
}
23+
24+
private List<Book> books = new ArrayList<>();
25+
26+
public List<Book> getAllBooks() {
27+
return books;
28+
}
29+
30+
public Book saveBook(Book book) {
31+
books.add(book);
32+
return book;
33+
}
34+
35+
public Book updateBook(Book book) {
36+
// check book contains new data then update otherwise return
37+
books.add(book);
38+
return book;
39+
}
40+
41+
public Book deleteBookByIsbn(String isbn) {
42+
int i = books.indexOf(new Book(isbn));
43+
return books.remove(i);
44+
}
45+
46+
public List<Book> searchBook(String keyword, int limit) {
47+
// Search DB for book title containing 'keyword' and return
48+
// result page of size 'limit'
49+
return new ArrayList<>();
50+
}
51+
52+
public List<Book> getAllNewBooks() {
53+
return new ArrayList<>();
54+
}
55+
56+
public List<Book> getBookBy(String author, String category, String language) {
57+
return new ArrayList<>();
58+
}
59+
}

jax-rs-context/src/main/java/com/readlearncode/restserver/BookResource.java

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import javax.ws.rs.core.GenericEntity;
66
import javax.ws.rs.core.MediaType;
77
import javax.ws.rs.core.Response;
8-
import java.util.ArrayList;
98
import java.util.List;
109

1110
/**
@@ -21,6 +20,7 @@ public class BookResource {
2120
private BookRepository bookRepository;
2221

2322
@GET
23+
@Path("all-books")
2424
@Produces(MediaType.APPLICATION_JSON)
2525
public Response getAllBooks() {
2626
List<Book> books = bookRepository.getAllBooks(); // queries database for all books
@@ -30,6 +30,7 @@ public Response getAllBooks() {
3030
}
3131

3232
@GET
33+
@Path("all-new-books")
3334
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
3435
// @Produces({"application/json", "application/xml"})
3536
public Response getAllNewBooks() {
@@ -41,6 +42,7 @@ public Response getAllNewBooks() {
4142
}
4243

4344
@GET
45+
@Path("book-by")
4446
@Produces(MediaType.APPLICATION_JSON)
4547
public Response getBookBy(@MatrixParam("author") String author,
4648
@MatrixParam("category") String category,
@@ -53,18 +55,21 @@ public Response getBookBy(@MatrixParam("author") String author,
5355
}
5456

5557
@GET
58+
@Path("cart")
5659
@Produces(MediaType.APPLICATION_JSON)
5760
public Response getCart(@CookieParam("cartId") int cartId) {
5861
return Response.ok().build();
5962
}
6063

6164
@GET
65+
@Path("referrer")
6266
@Produces(MediaType.APPLICATION_JSON)
6367
public Response getReferrer(@HeaderParam("referer") String referrer) {
6468
return Response.ok(referrer).build();
6569
}
6670

6771
@POST
72+
@Path("save-book")
6873
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
6974
@Produces(MediaType.APPLICATION_JSON)
7075
public Response saveBook(Book book) {
@@ -73,6 +78,7 @@ public Response saveBook(Book book) {
7378
}
7479

7580
@POST
81+
@Path("save")
7682
@Produces(MediaType.APPLICATION_JSON)
7783
public Response saveBookF(@FormParam("title") String title,
7884
@FormParam("author") String author,
@@ -115,66 +121,4 @@ public Response searchBook(@QueryParam("keyword") String keyword, @QueryParam("l
115121
}).build();
116122
}
117123

118-
119-
public class BookRepository {
120-
121-
private List<Book> books = new ArrayList<>();
122-
123-
public List<Book> getAllBooks() {
124-
return books;
125-
}
126-
127-
public Book saveBook(Book book) {
128-
books.add(book);
129-
return book;
130-
}
131-
132-
public Book updateBook(Book book) {
133-
// check book contains new data then update otherwise return
134-
books.add(book);
135-
return book;
136-
}
137-
138-
public Book deleteBookByIsbn(String isbn) {
139-
int i = books.indexOf(new Book(isbn));
140-
return books.remove(i);
141-
}
142-
143-
public List<Book> searchBook(String keyword, int limit) {
144-
// Search DB for book title containing 'keyword' and return
145-
// result page of size 'limit'
146-
return new ArrayList<>();
147-
}
148-
149-
public List<Book> getAllNewBooks() {
150-
return new ArrayList<>();
151-
}
152-
153-
public List<Book> getBookBy(String author, String category, String language) {
154-
return new ArrayList<>();
155-
}
156-
}
157-
158-
159-
public class Book {
160-
161-
private String isbn;
162-
private String title;
163-
private String author;
164-
private Float price;
165-
166-
public Book() {
167-
}
168-
169-
public Book(String isbn) {
170-
this.isbn = isbn;
171-
}
172-
173-
public Book(String title, String author, Float price) {
174-
this.title = title;
175-
this.author = author;
176-
this.price = price;
177-
}
178-
}
179-
180124
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.readlearncode.restserver.genericlist;
2+
3+
import com.readlearncode.restserver.Book;
4+
import com.readlearncode.restserver.BookRepository;
5+
6+
import javax.inject.Inject;
7+
import javax.ws.rs.*;
8+
import javax.ws.rs.core.GenericEntity;
9+
import javax.ws.rs.core.MediaType;
10+
import javax.ws.rs.core.Response;
11+
import java.util.List;
12+
13+
/**
14+
* Source code github.com/readlearncode
15+
*
16+
* @author Alex Theedom www.readlearncode.com
17+
* @version 1.0
18+
*/
19+
@Path("/my-books")
20+
public class BookResource {
21+
22+
@Inject
23+
private BookRepository bookRepository;
24+
25+
@GET
26+
@Path("response")
27+
@Produces(MediaType.APPLICATION_JSON)
28+
public Response getAllBooksResponse() {
29+
List<Book> books = bookRepository.getAllBooks(); // queries database for all books
30+
GenericEntity<List<Book>> list = new GenericEntity<List<Book>>(books) {
31+
};
32+
return Response.ok(list).build();
33+
}
34+
35+
@GET
36+
@Path("raw")
37+
@Produces(MediaType.APPLICATION_JSON)
38+
public List<Book> getAllBooksRaw() {
39+
List<Book> books = bookRepository.getAllBooks(); // queries database for all books
40+
return books;
41+
}
42+
43+
@GET
44+
@Path("raw-response")
45+
@Produces(MediaType.APPLICATION_JSON)
46+
public Response getAllBooksRawResponse() {
47+
List<Book> books = bookRepository.getAllBooks(); // queries database for all books
48+
return Response.ok(books).build();
49+
}
50+
}

0 commit comments

Comments
 (0)