2017-09-14 271 views
0

我是angularjs的新手。我想在$ http.put請求發送一個JSON對象如下:

function LoginCtrl($http){ 
this.login = function(){ 
    var self = this; 
     self.user = { 
      username: this.username, 
      password: this.password 
     } 
    $http.put('http://localhost:8086/app/login',self.user).then(function 
successCallback(response) { 
     }, function errorCallback(response) { 
     }) 
} 
} 

我只是想在REST API此JSON對象,該代碼是如下: @Path(「/應用程序「) 公共類LoginResource {

public LoginResource() { 
} 

@PUT 
@Path("/login") 
@Consumes(MediaType.APPLICATION_JSON) 
public Response getUserByName() { 
    return Response.status(Response.Status.ACCEPTED).build(); 
} 

我需要什麼參數傳遞給這個getUserByName API?我正在使用dropwizard

此外,如果有人可以告訴如何將index.html頁面設置爲jetty服務器配置中的起始頁面。

回答

1

首先,在你Dropwizard(新澤西州)後端定義輸入

public Response getUserByName(User user) { ... 

然後你就可以在你的JSON映射到一個實體是這樣的:

public class User { 
    @JsonProperty("name") 
    private String name; } 

在angularJS通過JSON對象作爲請求有效載荷$ http.PUT(uri,<JsonObject>

+0

在後端我使用dropwizard –

+0

那麼你應該需要@RequestBody註解。在你的請求對象中,你可以使用@JsonProperty(「name」)來映射事物。 – vilzu

+0

其實我需要在我的webmethod中獲取請求負載。有沒有辦法做到這一點? –

0
http.put(url, JSON.stringify(self.user), function (err, response) 
{ 
    // do something here 
} 
+0

以及如何獲得它在Response getUserByName ?? ?? –

+0

你的意思是把你想要通過http.get獲取記錄? –

+0

放置請求後,您的數據在您的數據庫中更新。因此,獲取請求將選擇更新的數據。 http.get(url,false,function(err,response){} –