GithubHelp home page GithubHelp logo

Comments (3)

ShawnyXiao avatar ShawnyXiao commented on May 29, 2024

感谢您的支持!

关于您说的这种关联查询的情况,我可能会这样设计:对于 /bookstores URI 的 GET 请求,添加请求参数以区分是否关联。大致设计可能如同下面这个控制器:

@RestController
@RequestMapping("/book_stores")
public class BookStoreController {

    private BookStoreService booStoreService;

    @Autowired
    public BookStoreController(BookStoreService booStoreService) {
        this.booStoreService= booStoreService;
    }

    // 其他方法...

    @GetMapping("/{bookStoreId}")
    public ResponseEntity<?> getBookStoreById(@PathVariable Long bookStoreId,
                                              @RequestParam(value = "verbose", required = false, defaultValue = "false") boolean verbose) {
        (verbose ? bookStoreService.getBookStoreWithBooksById(bookStoreId) : bookStoreService.getBookStoreById(bookStoreId))
                .map(ResponseEntity::ok)
                .orElseThrow(() -> new ResourceNotFoundException()
                        .setResourceName(ResourceNameConstant.BOOK_STORE)
                        .setId(bookStoreId));
    }

    // 其他方法...

}

这段代码可能并不能直接运行,需要做出相应的改动,例如:需要新增 ResourceNameConstant.BOOK_STORE

from springboot-mybatis.

oneone1995 avatar oneone1995 commented on May 29, 2024

@ShawnyXiao 非常感谢得到作者的回复,并附上了代码示例,觉得这个解决方案也很好。
另外想再交流一个问题,当get请求的参数巨多的时候,例如这个demo中的name、author、price、topic、publish_date(可能还不算多,但实际中确实碰到过比这更多的参数),而我有两个需求,一个是根据name和author搜索,另一个需求则是根据剩下所有条件搜索,在参数超级多的情况下又该如何设计?
在网上搜索这个问题,有讨伐restful的局限性和在这种情况下变通为post请求。
例如:https://www.zhihu.com/question/36706936
也搜索到在restful前提下的php框架yii2给出的解决方案,水平有限,没有通过研究过yii2的解决方案来试图在java下给出通样的方法。
链接:https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/rest-resources.md
想请教作者在这种情况下会如何设计?

from springboot-mybatis.

ShawnyXiao avatar ShawnyXiao commented on May 29, 2024

对于 GET 请求的参数过多的情况,实际上是不必担心的。因为,HTTP 协议中并没有255个字符的限制。但是在某些软件组件(例如某些旧版的浏览器)中可能有限制。RFC 7230 中有这样的描述:

Various ad hoc limitations on request-line length are found in practice. It is RECOMMENDED that all HTTP senders and recipients support, at a minimum, request-line lengths of 8000 octets.

翻译过来大概是这样:

在实践中,对请求行的长度有着各种特定限制。建议所有 HTTP 发送者和接受者至少支持高达 8000 个八位字节的请求行长度。

于是,我也去感受了一下,测试我的浏览器是否会限制 URI 的长度,发现并没有这个问题(测试了300个以上的字符)。而且对于 RESTful URI 的请求一般存在于各个客户端的代码中,并非直接使用浏览器地址栏,这样就更加不必担心了。

因此,可以了解到,我们的项目支持 GET 请求参数多的情景,并不需要特殊设计。

from springboot-mybatis.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.