Skip to content

Commit 92a1b2d

Browse files
author
Reza Rahman
committed
Got persistence working.
1 parent fce668f commit 92a1b2d

16 files changed

Lines changed: 348 additions & 23 deletions

File tree

actionbazaar/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444
<version>2.5.1</version>
4545
<scope>compile</scope>
4646
</dependency>
47+
<dependency>
48+
<groupId>org.glassfish.jersey.containers</groupId>
49+
<artifactId>jersey-container-servlet-core</artifactId>
50+
<version>2.0</version>
51+
<type>jar</type>
52+
<scope>provided</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.glassfish.jersey.media</groupId>
56+
<artifactId>jersey-media-moxy</artifactId>
57+
<version>2.0</version>
58+
<type>jar</type>
59+
<scope>provided</scope>
60+
</dependency>
4761
<dependency>
4862
<groupId>junit</groupId>
4963
<artifactId>junit</artifactId>

actionbazaar/src/main/java/com/actionbazaar/application/BidService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.actionbazaar.domain.Bid;
44

55
public interface BidService {
6-
public void addBid(Bid bid);
6+
public Bid addBid(Bid bid);
77

88
public Bid getBid(Long id);
99

actionbazaar/src/main/java/com/actionbazaar/application/DefaultBidService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class DefaultBidService implements BidService {
1313
private BidDao bidDao;
1414

1515
@Override
16-
public void addBid(Bid bid) {
17-
bidDao.addBid(bid);
16+
public Bid addBid(Bid bid) {
17+
return bidDao.addBid(bid);
1818
}
1919

2020
@Override

actionbazaar/src/main/java/com/actionbazaar/interfaces/rest/BidRestService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,27 @@
1515
import com.actionbazaar.domain.Bid;
1616

1717
@Stateless
18-
@Path("/bid")
18+
@Path("/bids")
1919
public class BidRestService {
2020

2121
@EJB
2222
private BidService bidService;
2323

2424
@POST
25-
@Consumes("text/xml")
25+
@Consumes("application/json")
2626
public void addBid(Bid bid) {
2727
bidService.addBid(bid);
2828
}
2929

3030
@GET
31-
@Produces("text/xml")
31+
@Produces("application/json")
32+
3233
public Bid getBid(@QueryParam("id") Long id) {
3334
return bidService.getBid(id);
3435
}
3536

3637
@PUT
37-
@Consumes("text/xml")
38+
@Consumes("application/json")
3839
public void updateBid(@QueryParam("id") Long id, Bid bid) {
3940
bidService.updateBid(bid);
4041
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.actionbazaar.interfaces.rest;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import javax.ws.rs.ext.ContextResolver;
6+
import javax.ws.rs.ext.Provider;
7+
import org.glassfish.jersey.moxy.json.MoxyJsonConfig;
8+
9+
@Provider
10+
// TODO See if this can be removed.
11+
public class JsonMoxyConfigurationContextResolver
12+
implements ContextResolver<MoxyJsonConfig> {
13+
14+
@Override
15+
public MoxyJsonConfig getContext(Class<?> objectType) {
16+
MoxyJsonConfig configuration = new MoxyJsonConfig();
17+
18+
Map<String, String> namespacePrefixMapper = new HashMap<>(1);
19+
namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
20+
configuration.setNamespacePrefixMapper(namespacePrefixMapper);
21+
configuration.setNamespaceSeparator(':');
22+
23+
return configuration;
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.actionbazaar.interfaces.rest;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
5+
import org.glassfish.jersey.moxy.json.MoxyJsonFeature;
6+
import org.glassfish.jersey.server.ResourceConfig;
7+
import org.glassfish.jersey.server.ServerProperties;
8+
9+
/**
10+
* JAX-RS configuration.
11+
*/
12+
@ApplicationPath("rest")
13+
public class RestConfiguration extends ResourceConfig {
14+
15+
public RestConfiguration() {
16+
// Resources
17+
packages(new String[] { BidRestService.class.getPackage().getName() });
18+
// Enable Bean Validation error messages.
19+
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
20+
// Providers - JSON.
21+
register(new MoxyJsonFeature());
22+
register(new JsonMoxyConfigurationContextResolver()); // TODO See if
23+
// this can be
24+
// removed.
25+
}
26+
}

actionbazaar/src/main/java/com/actionbazaar/repository/BidDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.actionbazaar.domain.Bid;
44

55
public interface BidDao {
6-
public void addBid(Bid bid);
6+
public Bid addBid(Bid bid);
77

88
public Bid getBid(Long id);
99

actionbazaar/src/main/java/com/actionbazaar/repository/DefaultBidDao.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ public class DefaultBidDao implements BidDao {
1111
private EntityManager entityManager;
1212

1313
@Override
14-
public void addBid(Bid bid) {
14+
public Bid addBid(Bid bid) {
1515
entityManager.persist(bid);
16+
17+
return bid;
1618
}
1719

1820
@Override

actionbazaar/src/main/resources/META-INF/persistence.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
55
<persistence-unit name="action-bazaar-unit">
6-
<jta-data-source>java:comp/env/jdbc/ActionBazaarDB</jta-data-source>
6+
<jta-data-source>jdbc/ActionBazaarDB</jta-data-source>
77
<properties>
88
<property name="javax.persistence.schema-generation.database.action"
99
value="drop-and-create" />

actionbazaar/src/main/webapp/WEB-INF/web.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,4 @@
1919
<welcome-file-list>
2020
<welcome-file>add_bid.jsf</welcome-file>
2121
</welcome-file-list>
22-
<data-source>
23-
<name>java:comp/env/jdbc/ActionBazaarDB</name>
24-
<class-name>org.apache.derby.jdbc.EmbeddedDriver</class-name>
25-
<url>jdbc:derby:/tmp/actionbazaar-database;create=true
26-
</url>
27-
</data-source>
2822
</web-app>

0 commit comments

Comments
 (0)