2017-03-21 4 views
1

私は簡単なスプリングブートの安静時のアプリケーションを持っています。私はコントローラ層とリポジトリ層を持ちますが、サービス層はありません。Controller-layerには何が属し、Service-layerには何がありますか?

@RequestMapping(value = "users/{id}", method = RequestMethod.GET) 
public Resource<UserResource> get(@PathVariable Long id) throws NotFoundException { 
    log.info("Invoked method: get with ID: " + id); 
    log.warn("Searching for user with ID " + id); 
    User user = userRepository.findOne(id); 
    if (user == null){ 
     log.error("Unexpected error, User with ID " + id + " not found"); 
     throw new NotFoundException("User with ID " + id + " not found"); 
    } 
    log.info("User found. Sending request back. ID of user is " + id); 
    return new Resource<UserResource>(getUserResource(user)); 
} 

私はService-Layerが存在しないため、私のコントローラーは私にとってビジネスロジックを実行します。今私はサービス層を実装したいと思います。私のコントローラーはどうしたらいいですか?

サービスレイヤー(今実装したい)はすべて作業を行い、コントローラーはサービスレイヤーに要求を委任するだけですか?

+2

それほど長い前、私は大規模な自動車メーカーのための大きなプロジェクトで働いていました。コントローラ層、サービス層、DAO層(データアクセスオブジェクト - DB照会用)がありました。ロジックは次のとおりです。コントローラーは要求を受信し、サービスは操作を行い、必要に応じてDAOを呼び出し、結果をコントローラーに返します。 DAOはDBにアクセスしてクエリを実行します。コントローラーレイヤーは非常に軽くなければならず、コントローラーはサービスに委託してビューをロードするだけでした。 DAOはクエリを実行し、サービスは残りのすべての作業をコントローラからのデータで行いました。 – BackSlash

+1

あなたが言ったようにサービスに実装すべきいくつかのbussinessロジックが必要な場合は、この単純なケースでは 'User user = userService.findOne(id);'が必要です。 –

+0

つまり、コントローラは何もしないでください。要求を受信して​​返信するだけです。両方ともありがとうございます:) –

答えて

1

サービスクラス

public class UserService{ 

public User findUser(String id){ 
     log.info("Invoked method: get with ID: " + id); 
     log.warn("Searching for user with ID " + id); 
     User user = userRepository.findOne(id); 
     if (user == null){ 
     log.error("Unexpected error, User with ID " + id + " not found"); 
     throw new NotFoundException("User with ID " + id + " not found"); 
     } 
    log.info("User found. Sending request back. ID of user is " + id); 
    return user; 
    } 

}

APIクラス

@RequestMapping(value = "users/{id}", method = RequestMethod.GET) 
public Resource<UserResource> get(@PathVariable Long id) throws NotFoundException { 
     return new Resource<UserResource>(userService.findUser(id)); 
} 
} 

適切なエラーページにリダイレクトするために、一般的な例外ハンドラNotFoundExceptionを追加します。

+0

サービス層では、if(ユーザー== null){...} 'を作ることはありません。なぜなら、時にはユーザーnullを受け取る必要があるからです(私は、コントローラ層でこの制御を行う必要があります。 –

2

質問:別のビュー/トランスポート/プロトコルの結果をレンダリングする場合、変更する必要があるのは自分ですか?それはコントローラに属します。

コントローラレイヤのコードは、サービスレイヤとビュー/トランスポート/プロトコル(必要に応じて)との間のビジネス入出力のマッピングにのみ関係する必要があります。これは、ビジネス・データをJSONにマッピングする(ビジネス/サービス・レイヤーがJSONなどで直接動作するのは無理ではない)、XML、HTMLなど、コンテンツ・タイプがHTTPの場合にマッピングすることを含む場合もあります。

コントローラの軽量化を感じるかもしれませんが、コントローラのバッキングは大部分の作業を行います。このような「シンプルな」コントローラは、フレームワークが認識してより重いボイラーあなたの利益のために、あなたのコードを外してください。時々、あなたは、ユーザーのヌルを受信する必要があるため

@Jaiに基づき
1
public class UserService{ 

    public User findUser(String id){ 
      log.info("Invoked method: get with ID: " + id); 
      log.warn("Searching for user with ID " + id); 
      User user = userRepository.findOne(id); 
      log.info("User found. Sending request back. ID of user is " + id); 
      return user; 
     } 
    } 




@RequestMapping(value = "users/{id}", method = RequestMethod.GET) 
public Resource<UserResource> get(@PathVariable Long id) throws NotFoundException { 
     User user = userService.findUser(id) 
     if (user == null){ 
      log.error("Unexpected error, User with ID " + id + " not found"); 
      throw new NotFoundException("User with ID " + id + " not found"); 
     } 
     return new Resource<UserResource>(getUserResource(user)); 
} 
} 

が唯一の違いは、応答が、あればある

public void anotherMethod(@PathVariable Long id){ 
    User user = userService.findOne(id); 
    if(user == null) { 
     //in this case I don't want to throw NotFoundException and make some other logic like create the user for example. 
    } 
} 
関連する問題