Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Repository("mealService")
- @Transactional
- public class MealServiceImpl implements MealService {
- @PersistenceContext
- private EntityManager em;
- @Override
- @Transactional(rollbackFor = Exception.class)
- public Meal addMeal(Meal meal) throws MealException {
- if (null == meal) {
- throw new MealException("Book data can not be null.");
- }
- try {
- em.persist(meal);
- return meal;
- } catch (Exception e) {
- if (e.getCause() instanceof ConstraintViolationException) {
- throw new MealExistException(e.getCause().getCause().getMessage());
- }
- throw new MealException(e.getMessage(), e);
- }
- }
- @Override
- public Meal getMeal(Long id) {
- if (null == id || id.longValue() < 1) {
- throw new IllegalArgumentException("ID can not be 0 or <0");
- }
- return em.find(Meal.class, id);
- }
- @Override
- public Meal deleteMeal(Long id) {
- if (null == id || id.longValue() < 1) {
- throw new IllegalArgumentException("ID can not be 0 or <0");
- }
- Meal meal = em.find(Meal.class, id);
- em.remove(meal);
- return meal;
- }
- }
- @WebService(serviceName = "mealServices", targetNamespace = "http://www.27programs.com/spring-cxf-rest/services")
- @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
- @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
- public class MealResources {
- @Autowired
- private MealServiceImpl mealService;
- @POST
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces({ MediaType.APPLICATION_JSON })
- @Path("/")
- public Response addBook(Meal meal) {
- try {
- return Response.status(HttpServletResponse.SC_OK).entity(mealService.addMeal(meal)).build();
- } catch (MealException e) {
- if ((e.getCause() instanceof PersistenceException)
- && (e.getCause().getCause() instanceof PropertyValueException)) {
- return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity(e.getMessage()).build();
- }
- return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
- } catch (PropertyValueException e) {
- return Response.status(HttpServletResponse.SC_BAD_REQUEST).entity(e.getMessage()).build();
- } catch (RuntimeException e) {
- return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
- }
- }
- @GET
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- @Produces({ MediaType.APPLICATION_JSON })
- @Path("/{id}")
- public Response getBook(@PathParam("id") String id) {
- Response response;
- try {
- Meal meal = mealService.getMeal(Long.valueOf(id));
- if (meal != null) {
- response = Response.status(HttpServletResponse.SC_OK).entity(meal).build();
- } else {
- response = Response.status(HttpServletResponse.SC_NOT_FOUND).build();
- }
- } catch (IllegalArgumentException e) {
- response = Response.status(HttpServletResponse.SC_NOT_FOUND).build();
- }
- return response;
- }
- @DELETE
- @Path("/delete/{id}")
- public void deleteBook(@PathParam("id") String id) {
- try {
- Meal meal = mealService.deleteMeal(Long.valueOf(id));
- if (meal != null) {
- System.out.print(Response.status(HttpServletResponse.SC_OK).entity(meal).build());
- } else {
- System.out.print(Response.status(HttpServletResponse.SC_NOT_FOUND).entity(meal).build());
- }
- } catch (IllegalArgumentException e) {
- System.out.print(Response.status(HttpServletResponse.SC_NOT_FOUND).build());
- }
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cxf="http://cxf.apache.org/core"
- xsi:schemaLocation="http://www.springframework.org/schema/jdbc
- http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.1.xsd
- http://cxf.apache.org/jaxrs
- http://cxf.apache.org/schemas/jaxrs.xsd
- http://cxf.apache.org/core
- http://cxf.apache.org/schemas/core.xsd
- ">
- <context:property-placeholder />
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver" />
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/store" />
- <property name="properties">
- <props>
- <prop key="user">root</prop>
- <prop key="password">root</prop>
- <prop key="c3p0.unreturnedConnectionTimeout">60</prop>
- <prop key="c3p0.testConnectionOnCheckout">true</prop>
- </props>
- </property>
- </bean>
- <bean id="emf"
- class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="jpaVendorAdapter">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
- </property>
- <property name="packagesToScan" value="com.store" />
- <property name="jpaProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
- <prop key="hibernate.max_fetch_depth">3</prop>
- <prop key="hibernate.jdbc.fetch_size">50</prop>
- <prop key="hibernate.jdbc.batch_size">10</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.hbm2ddl.auto">validate</prop>
- </props>
- </property>
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="emf" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- <context:annotation-config />
- <context:component-scan base-package="com.store" />
- <bean id="mealResources" class="com.store.openapi.services.MealResources" />
- <jaxrs:server id="mealResourcesServer" address="/meal">
- <jaxrs:providers>
- <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
- </jaxrs:providers>
- <jaxrs:serviceBeans>
- <ref bean="mealResources" />
- </jaxrs:serviceBeans>
- <jaxrs:features>
- <cxf:logging />
- </jaxrs:features>
- <jaxrs:extensionMappings>
- <entry key="xml" value="application/xml" />
- <entry key="json" value="application/json" />
- </jaxrs:extensionMappings>
- </jaxrs:server>
- </beans>
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <display-name>store</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <session-config>
- <session-timeout>5</session-timeout>
- </session-config>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value> classpath*:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/rest/*</url-pattern>
- </servlet-mapping>
- </web-app>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement