Advertisement
Guest User

Untitled

a guest
May 26th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. @Repository("mealService")
  2. @Transactional
  3. public class MealServiceImpl implements MealService {
  4.  
  5. @PersistenceContext
  6. private EntityManager em;
  7.  
  8. @Override
  9. @Transactional(rollbackFor = Exception.class)
  10. public Meal addMeal(Meal meal) throws MealException {
  11. if (null == meal) {
  12. throw new MealException("Book data can not be null.");
  13. }
  14.  
  15. try {
  16. em.persist(meal);
  17. return meal;
  18. } catch (Exception e) {
  19. if (e.getCause() instanceof ConstraintViolationException) {
  20. throw new MealExistException(e.getCause().getCause().getMessage());
  21. }
  22. throw new MealException(e.getMessage(), e);
  23. }
  24. }
  25.  
  26. @Override
  27. public Meal getMeal(Long id) {
  28. if (null == id || id.longValue() < 1) {
  29. throw new IllegalArgumentException("ID can not be 0 or <0");
  30. }
  31. return em.find(Meal.class, id);
  32. }
  33.  
  34. @Override
  35. public Meal deleteMeal(Long id) {
  36. if (null == id || id.longValue() < 1) {
  37. throw new IllegalArgumentException("ID can not be 0 or <0");
  38. }
  39. Meal meal = em.find(Meal.class, id);
  40. em.remove(meal);
  41. return meal;
  42. }
  43.  
  44. }
  45.  
  46. @WebService(serviceName = "mealServices", targetNamespace = "http://www.27programs.com/spring-cxf-rest/services")
  47. @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  48. @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  49. public class MealResources {
  50.  
  51. @Autowired
  52. private MealServiceImpl mealService;
  53.  
  54. @POST
  55. @Consumes(MediaType.APPLICATION_JSON)
  56. @Produces({ MediaType.APPLICATION_JSON })
  57. @Path("/")
  58. public Response addBook(Meal meal) {
  59.  
  60. try {
  61. return Response.status(HttpServletResponse.SC_OK).entity(mealService.addMeal(meal)).build();
  62. } catch (MealException e) {
  63. if ((e.getCause() instanceof PersistenceException)
  64. && (e.getCause().getCause() instanceof PropertyValueException)) {
  65. return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity(e.getMessage()).build();
  66. }
  67.  
  68. return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
  69. } catch (PropertyValueException e) {
  70. return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity(e.getMessage()).build();
  71. } catch (RuntimeException e) {
  72. return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
  73. }
  74. }
  75.  
  76. @GET
  77. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  78. @Produces({ MediaType.APPLICATION_JSON })
  79. @Path("/{id}")
  80. public Response getBook(@PathParam("id") String id) {
  81. Response response;
  82. try {
  83. Meal meal = mealService.getMeal(Long.valueOf(id));
  84. if (meal != null) {
  85. response = Response.status(HttpServletResponse.SC_OK).entity(meal).build();
  86. } else {
  87. response = Response.status(HttpServletResponse.SC_NOT_FOUND).build();
  88. }
  89. } catch (IllegalArgumentException e) {
  90. response = Response.status(HttpServletResponse.SC_NOT_FOUND).build();
  91. }
  92. return response;
  93. }
  94.  
  95. @DELETE
  96. @Path("/delete/{id}")
  97. public void deleteBook(@PathParam("id") String id) {
  98. try {
  99. Meal meal = mealService.deleteMeal(Long.valueOf(id));
  100. if (meal != null) {
  101. System.out.print(Response.status(HttpServletResponse.SC_OK).entity(meal).build());
  102. } else {
  103. System.out.print(Response.status(HttpServletResponse.SC_NOT_FOUND).entity(meal).build());
  104. }
  105. } catch (IllegalArgumentException e) {
  106. System.out.print(Response.status(HttpServletResponse.SC_NOT_FOUND).build());
  107. }
  108. }
  109.  
  110. }
  111.  
  112. <?xml version="1.0" encoding="UTF-8"?>
  113. <beans xmlns="http://www.springframework.org/schema/beans"
  114. xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  115. xmlns:context="http://www.springframework.org/schema/context"
  116. xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  117. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cxf="http://cxf.apache.org/core"
  118. xsi:schemaLocation="http://www.springframework.org/schema/jdbc
  119. http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
  120. http://www.springframework.org/schema/beans
  121. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  122. http://www.springframework.org/schema/tx
  123. http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
  124. http://www.springframework.org/schema/context
  125. http://www.springframework.org/schema/context/spring-context-4.1.xsd
  126. http://cxf.apache.org/jaxrs
  127. http://cxf.apache.org/schemas/jaxrs.xsd
  128. http://cxf.apache.org/core
  129. http://cxf.apache.org/schemas/core.xsd
  130. ">
  131.  
  132. <context:property-placeholder />
  133.  
  134. <import resource="classpath:META-INF/cxf/cxf.xml" />
  135. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  136.  
  137.  
  138. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  139. destroy-method="close">
  140. <property name="driverClass" value="com.mysql.jdbc.Driver" />
  141. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/store" />
  142.  
  143. <property name="properties">
  144. <props>
  145. <prop key="user">root</prop>
  146. <prop key="password">root</prop>
  147. <prop key="c3p0.unreturnedConnectionTimeout">60</prop>
  148. <prop key="c3p0.testConnectionOnCheckout">true</prop>
  149. </props>
  150. </property>
  151. </bean>
  152. <bean id="emf"
  153. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  154. <property name="dataSource" ref="dataSource" />
  155. <property name="jpaVendorAdapter">
  156. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
  157. </property>
  158. <property name="packagesToScan" value="com.store" />
  159. <property name="jpaProperties">
  160. <props>
  161. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  162. <prop key="hibernate.max_fetch_depth">3</prop>
  163. <prop key="hibernate.jdbc.fetch_size">50</prop>
  164. <prop key="hibernate.jdbc.batch_size">10</prop>
  165. <prop key="hibernate.show_sql">true</prop>
  166. <prop key="hibernate.hbm2ddl.auto">validate</prop>
  167. </props>
  168. </property>
  169. </bean>
  170.  
  171. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  172. <property name="entityManagerFactory" ref="emf" />
  173. </bean>
  174.  
  175. <tx:annotation-driven transaction-manager="transactionManager" />
  176.  
  177. <context:annotation-config />
  178. <context:component-scan base-package="com.store" />
  179.  
  180. <bean id="mealResources" class="com.store.openapi.services.MealResources" />
  181.  
  182. <jaxrs:server id="mealResourcesServer" address="/meal">
  183. <jaxrs:providers>
  184. <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
  185. </jaxrs:providers>
  186. <jaxrs:serviceBeans>
  187. <ref bean="mealResources" />
  188. </jaxrs:serviceBeans>
  189. <jaxrs:features>
  190. <cxf:logging />
  191. </jaxrs:features>
  192. <jaxrs:extensionMappings>
  193. <entry key="xml" value="application/xml" />
  194. <entry key="json" value="application/json" />
  195. </jaxrs:extensionMappings>
  196. </jaxrs:server>
  197. </beans>
  198.  
  199. <?xml version="1.0" encoding="UTF-8"?>
  200. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  201. <display-name>store</display-name>
  202. <welcome-file-list>
  203. <welcome-file>index.jsp</welcome-file>
  204. </welcome-file-list>
  205. <session-config>
  206. <session-timeout>5</session-timeout>
  207. </session-config>
  208. <context-param>
  209. <param-name>contextConfigLocation</param-name>
  210. <param-value> classpath*:applicationContext.xml</param-value>
  211. </context-param>
  212. <listener>
  213. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  214. </listener>
  215.  
  216. <servlet>
  217. <servlet-name>CXFServlet</servlet-name>
  218. <servlet-class>
  219. org.apache.cxf.transport.servlet.CXFServlet
  220. </servlet-class>
  221. <load-on-startup>1</load-on-startup>
  222. </servlet>
  223.  
  224. <servlet-mapping>
  225. <servlet-name>CXFServlet</servlet-name>
  226. <url-pattern>/rest/*</url-pattern>
  227. </servlet-mapping>
  228. </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement