Online Food Order Java SP
Online Food Order Java SP
Spring Boot
1. Project Overview
The app will allow users to browse through menus, select items, and place
orders online. There will be two key roles:
2. Tech Stack
b. Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/food_ordering_
db
spring.datasource.username=root
spring.datasource.password=rootpassword
spring.jpa.hibernate.ddl-auto=update
Create entity classes for User, Food Item, Order, and Menu. Use Spring Data
JPA for repository management.
@Entity
public class FoodItem {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
private String category;
private String description;
// Getters, setters, and constructors
}
d. Service Layer
@Service
public class FoodItemService {
@Autowired
private FoodItemRepository foodItemRepository;
e. Controller Layer
Expose REST APIs to handle requests for food items, users, and orders.
@RestController
@RequestMapping("/api/food")
public class FoodItemController {
@Autowired
private FoodItemService foodItemService;
@GetMapping("/items")
public ResponseEntity<List<FoodItem>> getAllFoodItems() {
return
ResponseEntity.ok(foodItemService.getAllFoodItems());
}
@PostMapping("/add")
public ResponseEntity<FoodItem> addFoodItem(@RequestBody
FoodItem foodItem) {
return
ResponseEntity.ok(foodItemService.saveFoodItem(foodItem));
}
}
f. Authentication and Authorization
● Use Spring Security with JWT for securing APIs. Create roles for Admin
and Customer.
● Define custom filters for JWT authentication.
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.csrf().disable()
.authorizeRequests()
.antMatchers("/api/admin/**").hasRole("ADMIN")
.antMatchers("/api/customer/**").hasRole("CUSTOMER")
.and().sessionManagement().sessionCreationPolicy(SessionCreation
Policy.STATELESS);
}
}
Integrate with Stripe for handling payments using their Java SDK.
Set up routing for different pages like Menu, Cart, and Order History.
function App() {
return (
<Router>
<Switch>
<Route path="/menu" component={Menu} />
<Route path="/cart" component={Cart} />
</Switch>
</Router>
);
}
Use Axios or Fetch API to make API calls to the Spring Boot backend.
useEffect(() => {
axios.get('/api/food/items')
.then(response => setFoodItems(response.data))
.catch(error => console.error(error));
}, []);
d. Cart Functionality
e. Payment Integration
5. Database Design
a. User Table
b. FoodItem Table
c. Order Table
d. OrderDetails Table
@RunWith(SpringRunner.class)
@WebMvcTest(FoodItemController.class)
public class FoodItemControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnAllFoodItems() throws Exception {
mockMvc.perform(get("/api/food/items"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(5)); //
Expecting 5 food items
}
}
FROM openjdk:17-jdk-slim
COPY target/online-food-ordering.jar /app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
For the frontend, use nginx or serve to host the React app.
c. Deploy to Cloud
Deploy the backend using Heroku, AWS Elastic Beanstalk, or Google Cloud
Run. For the frontend, deploy using Vercel or Netlify.