When do I use path params vs. query params in a RESTful API?

I want to make my RESTful API very predictable. What is the best practice for deciding when to make a segmentation of data using the URI rather than by using query params. It makes sense to me that system parameters that support pagination, sorting, and grouping be after the ‘?’ But what about fields like … Read more

How to upload a file and JSON data in Postman?

I am using Spring MVC and this is my method: /** * Upload single file using Spring Controller. */ @RequestMapping(value = “/uploadFile”, method = RequestMethod.POST) public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler( @RequestParam(“name”) String name, @RequestParam(“file”) MultipartFile file, HttpServletRequest request, HttpServletResponse response) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // Creating the directory … Read more

Spring MVC @PathVariable with dot (.) is getting truncated

This is continuation of question Spring MVC @PathVariable getting truncated Spring forum states that it has fixed(3.2 version) as part of ContentNegotiationManager. see the below link. https://jira.springsource.org/browse/SPR-6164 https://jira.springsource.org/browse/SPR-7632 In my application requestParameter with .com is truncated. Could anyone explain me how to use this new feature? how is it configurable at xml? Note: spring forum- … Read more

How to solve the “failed to lazily initialize a collection of role” Hibernate exception

I have this problem: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: mvc3.model.Topic.comments, no session or session was closed Here is the model: @Entity @Table(name = “T_TOPIC”) public class Topic { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @ManyToOne @JoinColumn(name=”USER_ID”) private User author; @Enumerated(EnumType.STRING) private Tag topicTag; private String name; private String text; @OneToMany(mappedBy = “topic”, … Read more

How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?

I’m using Spring MVC for a simple JSON API, with @ResponseBody based approach like the following. (I already have a service layer producing JSON directly.) @RequestMapping(value = “/matches/{matchId}”, produces = “application/json”) @ResponseBody public String match(@PathVariable String matchId) { String json = matchService.getMatchJson(matchId); if (json == null) { // TODO: how to respond with e.g. 400 … Read more

How does autowiring work in Spring?

I’m a little confused as to how the inversion of control (IoC) works in Spring. Say I have a service class called UserServiceImpl that implements UserService interface. How would this be @Autowired? And in my Controllers, how would I instantiate an instance of this service? Would I just do the following? UserService userService = new … Read more