2017-09-22 1 views
1

私はサーバ用にJaxRSジャージを使用しています。私はそれをawsにデプロイしました。サーバーへのHttp投稿要求は、郵便配達員とは連携していますが、Http投稿Apacheクライアントとは連携していません。 後HTTPポストマンからポストマンの作業はブラウザでは動作しません

@Path("/data") 
public class MyResource { 


    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public List<trackerdetails> getIt() { 
     SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory(); 
     Session session = sessionfactory.openSession(); 
     session.beginTransaction(); 
     trackerdetails user = new trackerdetails(); 
     List<trackerdetails> sendlist = (List<trackerdetails>) session.createQuery("from trackerdetails").list(); 

     session.getTransaction().commit(); 
     session.close(); 
     return sendlist; 
    } 

    @POST 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Produces(MediaType.APPLICATION_JSON) 
    public trackerdetails putit(trackerdetails track) { 
     track.setDate(new Date()); 
     SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory(); 
     Session session = sessionfactory.openSession(); 
     session.beginTransaction(); 

     session.save(track); 
     session.getTransaction().commit(); 
     session.close(); 
     return track; 
    } 

私のJavaの休息のためのサービスです後は、次の

@Entity 
@XmlRootElement 
public class trackerdetails { 
    @Id @GeneratedValue(strategy = GenerationType.AUTO) 
    private int autoid; 
    private String latitude; 
    private String longitude; 
    private String devicename; 
    private Date date; 
    public trackerdetails(){ 

    } 
    public int getAutoid() { 
     return autoid; 
    } 
    public void setAutoid(int autoid) { 
     this.autoid = autoid; 
    } 
    public String getLatitude() { 
     return latitude; 
    } 
    public void setLatitude(String latitude) { 
     this.latitude = latitude; 
    } 
    public String getLongitude() { 
     return longitude; 
    } 
    public void setLongitude(String longitude) { 
     this.longitude = longitude; 
    } 
    public String getDevicename() { 
     return devicename; 
    } 
    public void setDevicename(String devicename) { 
     this.devicename = devicename; 
    } 
    public Date getDate() { 
     return date; 
    } 
    public void setDate(Date date) { 
     this.date = date; 
    } 

私trackerdetailsクラスです後、私のクライアント側のHTTP POSTリクエスト

HttpPost httpPost = new HttpPost("myurl"); 
List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
nvps.add(new BasicNameValuePair("devicename", "vip")); 
nvps.add(new BasicNameValuePair("date", "hjksvn")); 
nvps.add(new BasicNameValuePair("latitude", "hello")); 
nvps.add(new BasicNameValuePair("longitude","hi")); 
httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 


httpPost.setHeader("Cache-Control", "no-cache"); 
httpPost.setHeader("Content-type", "application/json"); 
httpPost.setHeader("Host", "trackertest.herokuapp.com"); 


CloseableHttpResponse response2 = httpclient.execute(httpPost); 

try { 
    System.out.println(response2.getStatusLine()); 
    System.out.println(response2.toString()); 
    HttpEntity entity2 = response2.getEntity(); 
    // do something useful with the response body 
    // and ensure it is fully consumed 
     BufferedReader rd = new BufferedReader(
    new InputStreamReader(response2.getEntity().getContent())); 

StringBuffer result1 = new StringBuffer(); 
String line = ""; 
while ((line = rd.readLine()) != null) { 
    result1.append(line); 

     System.out.println(line); 
     System.out.println(""); 
} 
System.out.println(result1); 
    EntityUtils.consume(entity2); 
} finally { 
    response2.close(); 
} 

である私のエラーステータスが400 です悪い要求

説明クライアントから送信された要求が構文的に正しくありません。

+0

'HttpPost httpPost =新HttpPost( "myurl");' PLZ 'HttpPost httpPost =新しいHttpPost(myurl)に変更して違って見えます。 ' – Srinivasu

+0

URLは正しいです。私はURLを追加したことを意味しました – user2515173

答えて

0

REST APIはJSON形式でリクエストしますが、NameValuePairを使用してリクエストボディを構築する方法では、JSONという形式にはなりません。

あなたがJacksonようJSONにオブジェクトを変換することができ、いくつかのライブラリを使用することにより、有効なJSONリクエストボディのどちらかをしなければならないか、手動でJSONリクエストボディを構築して、あなたのAPIを呼び出すことができます。以下は

手動JSONリクエストボディを構築する一つの方法である -

HttpPost httpPost = new HttpPost("myurl"); 

StringBuilder jsonBody = new StringBuilder(); 
jsonBody.append("{"); 
jsonBody.append("\"devicename\" : ").append("\"vip\"").append(","); 
// Pass a valid date because on server side, you are using Date object for accepting it 
jsonBody.append("\"date\" : ").append("\"2017-09-23\"").append(","); 
jsonBody.append("\"latitude\" : ").append("\"vip\"").append(","); 
jsonBody.append("\"longitude\" : ").append("\"vip\""); 
jsonBody.append("}"); 

StringRequestEntity requestEntity = new StringRequestEntity(jsonBody.toString(),"application/json","UTF-8"); 


httpPost.setRequestEntity(requestEntity); 

httpPost.setHeader("Cache-Control", "no-cache"); 
httpPost.setHeader("Content-type", "application/json"); 
httpPost.setHeader("Host", "trackertest.herokuapp.com"); 
// Rest code should remain same 
関連する問題