jpa取出mysql数组_json返回嵌套数组而不是对象[spring boot+jpa+mysql+rest]

问题

大家好,请帮我解决这个问题。

我已经开始构建一个RESTAPI,在测试我创建的URL时遇到了一个问题。示例:当我发送获取一个对象列表的请求时,该请求工作正常,但JSON返回的数据语法不好:我得到了结果嵌套数组,而不是一个包含JSON对象的全局数组。请检查我的代码,我有两个实体,现在它们中的一个依赖于另一个,我使用@onetomany在它们之间建立关系,并且没有发生任何错误。事先谢谢。

解决方案

问题是:默认情况下,我的查询返回的是列表列表,因此我必须通过添加构造函数调用来修改查询。请检查此链接:

using new keyword in HQL query

另外,我添加了@jsonignore注释来忽略我的实体中的一些属性,以防止它们显示出来。现在数据按我想要的格式显示:d谢谢您的帮助。

Check the new result here

更新

您好,我最近意识到,使用@jsonignore注释来防止在JSON响应中发送某些属性是不好的,自定义要发送的属性的最佳方法是使用DTOS类。再次感谢

kj007

实体1

import java.util.List;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.OneToMany;

import javax.persistence.Table;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's an outline of how you can implement the shipping REST APIs using Spring Boot, Spring MVC, JPA/Batis, MySQL, and JWT: 1. Set up a Spring Boot project with the necessary dependencies for Spring MVC, JPA/Batis, MySQL, and JWT. You can use Spring Initializr to generate the project structure and add the dependencies. 2. Define the domain model for the shipping application, including entities such as Order, Product, Customer, and ShippingAddress. Map the entities to database tables using JPA annotations or MyBatis mapper XML files. 3. Implement the repository layer to perform CRUD operations on the database using JPA or MyBatis. You can use Spring Data JPA or MyBatis-Spring to simplify the implementation. 4. Define the REST API endpoints for the shipping application using Spring MVC annotations. Use JWT for authentication and authorization of the API endpoints. 5. Implement the service layer to perform business logic operations such as calculating shipping costs, validating orders, and processing payments. Use dependency injection to inject the repository and other services into the service layer. 6. Write unit tests to ensure that the application logic is working correctly. You can use JUnit and Mockito to write the tests. 7. Deploy the application to a server and test the API endpoints using a tool such as Postman. Here's some example code to get you started: ```java @RestController @RequestMapping("/api/orders") public class OrderController { @Autowired private OrderService orderService; @PostMapping("/") public ResponseEntity<Order> createOrder(@RequestBody Order order) { Order createdOrder = orderService.createOrder(order); return ResponseEntity.ok(createdOrder); } @GetMapping("/{id}") public ResponseEntity<Order> getOrder(@PathVariable("id") Long orderId) { Order order = orderService.getOrder(orderId); return ResponseEntity.ok(order); } // Other API endpoints for updating and deleting orders } @Service public class OrderService { @Autowired private OrderRepository orderRepository; public Order createOrder(Order order) { // Calculate shipping costs and validate the order order.setShippingCosts(10.0); order.setTotalPrice(order.getProducts().stream() .mapToDouble(Product::getPrice) .sum() + order.getShippingCosts()); return orderRepository.save(order); } public Order getOrder(Long orderId) { return orderRepository.findById(orderId) .orElseThrow(() -> new NotFoundException("Order not found")); } // Other service methods for updating and deleting orders } @Repository public interface OrderRepository extends JpaRepository<Order, Long> { } ``` This code defines a REST API endpoint for creating orders and getting orders by ID. The order creation logic is implemented in the OrderService class, which calculates shipping costs and saves the order to the database using the OrderRepository interface. JWT authentication and authorization can be added to the API endpoints using Spring Security.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值