Spring REST best practice for method types

I am building a Spring RESTfull service and a I have the following method that retrieves a Place object based on given zipcode:

@RequestMapping(value = "/placeByZip", method = RequestMethod.GET) public Place getPlaceByZipcode(@RequestParam(value="zipcode") String zipcode) { Place place = placeService.placeByZip(zipcode); return place; }

Is it best practice to have the return type of "Place"? I imagine this is difficult for error handling?

-------------Problems Reply------------

Using the latest versions of Spring for a RESTfull web service I do believe returning the 'Object' is good practise as it allows you to simplify your code and be specific on what you are returning. I see this as strongly typing your API response.

A good practise for error handling is to use the controller advice utility supplied by spring.

Have a read of:


This allows you to throw your exceptions at any of your service layers and produce a nice helpful error response.

A better practice would be to create a Data Transfer Object with only the properties you will be using in the front end.

With a proper JS framework you could easily do proper error handling. (for example you could define a service in AngjularJs which would define the DTO's fields).

Also, you might as well do return placeService.placeByZip(zipCode);

As robinsio suggested it is good practice to add controller advice. You can set the status to some http code (for example HttpStatus.conflict) and have an exception handler (ex. BaseServiceException) which you can throw inside your place service if some validation rules you define are broken.

The controller advice could return a map which you handle in the case of the respective http status code in a consistent manner (let's say a modal appears in the interface to notify of the message you sent from the base service exception).

Category:java Views:5 Time:2018-05-12
Tags: java rest spring

Related post

  • What are the best practices for safe type conversion in C#? 2010-08-11

    what are the best practices for type conversions in C# ? int temp=System.ConvertToInt32(Request.QueryString["Id"]); if (temp!=null) { // logic goes here } This fails if Id somehow turns out to be 'abc' Please advice the use of of ternary operators an

  • Best Practices for Content Types in SharePoint 2011-01-04

    Recently, we came across a severe problem in production farm with the Content Types. I would like to explain the background of this problem first. We have nice working feature for Content Types installation in production and test farms. We developed

  • Best practice for enforcing type safety in polymorphic inheritance hierarchies 2012-03-21

    I seem to run into this situation quite a lot and have yet to find a solution that I find acceptable. Quite often I will have parallel inheritance hierarchies where a method in one hierarchy gets passed the matching class from the other hierarchy as

  • REST best-practice for overlong URIs 2010-01-15

    I have REST services which should receive really long queries via GET. Say for example I want to query a service with many geographical coordinates to find out something about all this coordinates. 1) My first thought was to use long URIs and increas

  • REST best practice for accessing a resource using two attributes 2010-07-02

    I have unique RESTful resources which can only be identified in the system with two attributes. What I want to know is what is the best practice approach to do this. The resource is identified with an id attribute (which is not unique) and a name att

  • Best practice for method parameters 2011-04-21

    currently, in my team, we have very live debate about some issues in our asp.net mvc project. to cut a long story short, it's all about should we be using objects as a some kind of container for parameters to call a methods in our project...other sid

  • Best practice for method chaining ("return this") 2011-06-01

    I have an abstract class called DatabaseRow that, after being derived and constructed, is mainly loaded from a Load(object id) method. I have lots of code that creates a new instance of the class, loads it from an ID, then returns the class. I'd like

  • Best practice for returning type from SQL Server to ExecuteScalar? 2012-02-26

    In C# SqlCommand - ExecuteScalar is : private object CompleteExecuteScalar(SqlDataReader ds, bool returnSqlValue) now let's go to SQL Server. If I want to return a value from the select (which goes to ExecuteScalar), e.g. : if record already exists s

  • REST best practice for getting a subset list 2010-02-22

    I read the article at http://stackoverflow.com/questions/1296421/rest-complex-applications and it answers some of my questions, but not all. I am designing my first REST application and need to return "subset" lists to GET requests. Which of the foll

  • unit test best practice for method with mocks in Mockito 2011-04-20

    Lets say we have method to test in class A that calls method from class B. To test it we created mock for B and then verify if it was called. Is verify(...) enough for unit test or I need assert actual result of tested method? Below is simplified exa

  • Best Practice for calling a method programmatically where the methodName is -(void)animatePageX where x =1 thru 25 2011-10-18

    I am doing a children's book using Leaves. I want to have a different animated image and sound play for each page. I have figured out how to do the animation, I have it in a method that i can pass all the animation objects for each page. But each pag

  • Best Practice for Subset of Business Object Fields Structure? 2009-02-03

    If I have a business object with 50 fields, and I need to populate something like a drop down list or gridview with only 3 fields from the business object to allow quick browsing. Is it best practice to load the fully populated BO then just grab the

  • What is the best practice for domain model re-use in composite applications? 2008-12-29

    We have a composite application built using the Composite UI Application Block (CAB)/Smart Client Software Factory (SCSF). To date, each module in our composite app has used its own set of DTO's, and business logic has been duplicated throughout the

  • best practices for text-based searches in database 2012-01-23

    I have an application where I need to search in various text-based fields. The application is developed using NHibernate as an ORM. I would like to implement Porter Stemming in searches, in order to be able to return relevant results even when the ke

  • Best practice for Python process control 2012-04-07

    This is my first hack at doing any system-level programming (mostly a LAMPhp, specifically Drupal, web dev up to this point). Because of availability of a library with a very specific feature, I am using Python for an upcoming project. I need to run,

  • Rails Best Practices for RESTful controller CREATE and UPDATE methods 2010-09-03

    OK, I am trying to understand the best practices for the CREATE and UPDATE methods for both HTML and XML formats. The default code for a controller that the rails generator generates is a little unclear to me. For the CREATE method, given a good save

  • Best Practices for securing a REST API / web service 2008-08-11

    When designing a REST API or service are there any established best practices for dealing with security (Authentication, Authorization, Identity Management) ? When building a SOAP API you have WS-Security as a guide and much literature exists on the

  • What is the best practice for naming private and static private methods in C#? 2009-04-04

    I'm trying to figure out what is the smartest way to name private methods and private static methods in C#. Background: I know that the best practice for private members is underscore-prefix + camelcase. You could argue this with me, but trust me I'v

  • Best practice for partial updates in a RESTful service 2010-03-14

    I am writing a RESTful service for a customer management system and I am trying to find the best practice for updating records partially. For example, I want the caller to be able to read the full record with a GET request. But for updating it only c

  • Best practices for entitlement using spring security and/or shiro 2011-05-16

    I'm interested in opinions on the best way to handle the concept of "entitlement" using either Spring Security or Shiro. For example, imagine, say, a JAX-RS endpoint that has a signature like this: AccountDetails getAccountDetails(String accountId);

  • What are the best practices for designing a RESTful public API on Rails? 2008-09-27

    What are the best practices for this? Rails comes with RESTful resources out of the box, but do you use those for your actual public API? If so, how would you accomplish versioning of your API (i.e. example.com/api/v2/foo/bar, etc)? --------------Sol

  • Best practice for rate limiting users of a REST API? 2009-03-05

    I am putting together a REST API and as I'm unsure how it will scale or what the demand for it will be, I'd like to be able to rate limit uses of it as well as to be able to temporarily refuse requests when the box is over capacity or if there is som

  • best practice for passing many arguments to method? 2010-03-12

    Occasionally , we have to write methods that receive many many arguments , for example : public void doSomething(Object objA , Object objectB ,Date date1 ,Date date2 ,String str1 ,String str2 ) { } When I encounter this kind of problem , I often enca

  • Best Practice for Creating a Business Entity Helper Method 2010-05-24

    In terms of memory management, memory footprint and ease of developer use what would be the best practice for creating helper methods for a custom entity object. So I have my object and I need a get, save, get history, maybe even a find method. The t

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.109 (s). 11 q(s)