2017-10-23 6 views
0

私は、ConsignmentCodeという列を持つMySqlレコードテーブルを持っています。 ConsignmentCodeでテーブルを検索するHTTPリクエストを送信したいと思います。Springフレームワークを使用したPOST要求の送信

http://localhost:8081/records/consignmentCodeにPOSTリクエストをして体内に{consignmentCode: "123456789"}という名前を付けると、404 Not Foundエラーが表示されます。なぜ誰が知っていますか?

コントローラクラス:

/** 
    * POST request to search by ConsignmentCode 
    * 
    * /records/consignmentCode 
    * 
    * Input ex: {consignmentCode: "123456789"} 
    * @param params 
    * @return 
    */ 

    @CrossOrigin 
    @ResponseBody 
    @RequestMapping(
      value = "/consignmentCode", 
      method = RequestMethod.POST, 
      consumes= MediaType.APPLICATION_JSON_VALUE) 
    public java.lang.String SearchRecordsByConsignmentCode(@RequestBody String params) { 
     System.out.print("this SearchRecordsByConsignmentCode respond happened"); 
     JSONObject obj = new JSONObject(); 
     JSONObject jsonObj = new JSONObject(params); 
     String likeConsignment= jsonObj.getString("consignmentCode"); 

     List<record> results=RecordDao.SearchRecordsByConsignmentCode(likeConsignment); 
     obj.put("results", results); 
     return obj.toString(); 
    } 

ダオクラス:

/** 
    * Search by ConsignmentCode. 
    * 
    * @param consignmentCode 
    * @return 
    */ 

    public List<record> SearchRecordsByConsignmentCode(String consignmentCode) { 
     final String sql = "SELECT * FROM records WHERE ConsignmentCode = ?"; 
     List<record> Record = jdbcTemplate.query(sql, new RowMapper<record>() { 
      public record mapRow(ResultSet resultSet, int Id) throws SQLException { 
       record records = new record(); 
       records.setId(resultSet.getInt("Id")); 
       records.setNumber(resultSet.getString("Number")); 
       records.setTitle(resultSet.getString("Title")); 
       records.setScheduleId(resultSet.getInt("ScheduleId")); 
       records.setTypeId(resultSet.getInt("TypeId")); 
       records.setConsignmentCode(resultSet.getString("ConsignmentCode")); 
       records.setStateId(resultSet.getInt("StateId")); 
       records.setContainerId(resultSet.getInt("ContainerId")); 
       records.setLocationId(resultSet.getInt("LocationId")); 
       records.setCreatedAt(resultSet.getDate("CreatedAt")); 
       records.setUpdatedAt(resultSet.getDate("UpdatedAt")); 
       records.setClosedAt(resultSet.getDate("ClosedAt")); 
       System.out.print(records); 
       return records; 
      } 
     }, consignmentCode); 
     return Record; 
    } 
+0

あなたのDAOクラスはおそらくエラーとは関係ありません。ブレークポイントをSpreadのDispatcherServletに配置し、エラーがdoServiceメソッドをデバッグする場所を確認する必要があります。 –

答えて

0

あなたは文字列を受信し、JSONとしてパースすることを期待する必要はありません。これはSpring MVCの組み込みの動作です。

jackson-dependenciesをインポートするだけです。あなたは、Mavenを使用して、あなたのpom.xmlの依存関係に追加した場合:

<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-mapper-asl</artifactId> 
    <version>1.9.9</version> 
</dependency> 
<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-core-asl</artifactId> 
    <version>1.9.9</version> 
</dependency> 

次に、あなたのコントローラをこのように変更します。

@Controller 
public class RestController { 

    @ResponseBody 
    @RequestMapping(
      value = "/consignmentCode", 
      method = RequestMethod.POST, 
      consumes= MediaType.APPLICATION_JSON_VALUE) 
    public List<Record> SearchRecordsByConsignmentCode(@RequestBody ConsignmentCode code) { 
     System.out.print("this SearchRecordsByConsignmentCode respond happened"); 

     List<Record> results=RecordDao.SearchRecordsByConsignmentCode(code.getConsignmentCode()); 
     return results; 
    } 

あなたがメッセージボディをカプセル化し、このクラスを作成する必要があります。

import java.io.Serializable; 

public class ConsignmentCode implements Serializable { 

    private String consignmentCode; 

    public String getConsignmentCode() { 
     return consignmentCode; 
    } 

    public void setConsignmentCode(String consignmentCode) { 
     this.consignmentCode = consignmentCode; 
    } 


} 

そして、それを起動して、コントローラのURLにこのメッセージを投稿してみてください(へ/受諾のContent-Typeヘッダを設定することを忘れないでください)。プロパティ名は、ちょうどこの方法では、あまりにもセミコロンの間で行かなければならない:

{"consignmentCode":"123456789"} 

そして、あなたはJSON形式

1

使用で正しい結果が得られます:

@RequestBody String consignmentCode 

の代わり:

@RequestBody String params 
関連する問題