2017-09-14 272 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){} –