简体   繁体   English

为什么我的参数没有传递到RESTful Web服务?

[英]Why my parameters are not getting passed to the RESTful web service?

I have this code using Angular 4 HttpClient , supposed to communicate with a simple RESTful JAVA web service and get a JSON string back: 我有使用Angular 4 HttpClient这段代码,应该与一个简单的RESTful JAVA Web服务进行通信并返回一个JSON字符串:

this.client.post('http://localhost:8080/ToDoService/rest/service/create', {id: 'foo', todo: 'bar'}).subscribe((data) => {
    console.log("Got data: ", data);
}, (error) => {
    console.log("Error", error);           
})

It's not working. 没用 What I mean is that the id and todo parameters are not getting passed to the REST backend. 我的意思是idtodo参数没有传递到REST后端。

At the other hand, if I change above code to: 另一方面,如果我将以上代码更改为:

this.client.post('http://localhost:8080/ToDoService/rest/service/create?id=foo&todo=bar', '').subscribe((data) => {
    console.log("Got data: ", data);
}, (error) => {
    console.log("Error", error);           
})

Everything works fine, but I'm sure the second snipped is wrong. 一切正常,但是我敢肯定第二个错误是错误的。 It just looks wrong. 看起来很不对劲。

Could you give me a push and point my mistake? 你能给我个提示并指出我的错误吗?

Ps 聚苯乙烯

The JAVA backend: JAVA后端:

@Path("/service")
public class Rest extends Application{
    @POST
    @Path("/create")
        public Response printMessage(@QueryParam("id") String userId, @QueryParam("todo") String toDo) {
            JSONObject result = new JSONObject();

            result.put("id", userId);
            result.put("todo", toDo);

            return Response.status(200).entity(result.toString()).build();
        }
}

You're mapping QueryParams, you need to map that payload to either a Map or an Object: 您正在映射QueryParams,需要将该有效负载映射到Map或Object:

class PayLoad {
    private String id;
    private String todo;
    // Getter and Setters
}

@Path("/service")
public class Rest extends Application{
    @POST
    @Path("/create")
        public Response printMessage(@RequestBody PayLoad payload) {
            JSONObject result = new JSONObject();

            result.put("id", payload.getId());
            result.put("todo", payload.getTodo());

            return Response.status(200).entity(result.toString()).build();
        }
}

Hope this helps! 希望这可以帮助!

First of all, looking at the MyKong tutorial you can see that @QueryParam accepts parameters sent in the URL . 首先,在MyKong教程中,您可以看到@QueryParam接受URL中发送参数。 That is a first problem here. 这是第一个问题。 Back to the main point. 回到重点。


I am not an expert in Angular 4, but I think the problem lies deeper in your architecture. 我不是Angular 4的专家,但我认为问题出在您的架构中。 You are sending to your backend: 您正在发送到后端:

{id: 'foo', todo: 'bar'}

and expect in your Java: 并期望在您的Java中:

@QueryParam("id") String userId, @QueryParam("todo") String toDo

You pass an object and expect in your Java backend two strings . 您传递一个对象,并期望在Java后端有两个字符串 If you want to get your object, you might create this kind of class: 如果要获取对象,则可以创建此类:

public class JsonWrapper    {
    private String id;
    private String todo;

 // Classic constructor and setters
}

Then, in your service: 然后,为您服务:

@Path("/service")
public class Rest extends Application    {
    @POST
    @Path("/create")
    public Response printMessage(@RequestBody JsonWrapper wrapper) {
        JSONObject result = new JSONObject();

        result.put("id", wrapper.getId());
        result.put("todo", wrapper.getTodo());

        return Response.status(200).entity(result.toString()).build();
    }
}

If the JsonWrapper does not work, I think a Map can do the trick, too. 如果JsonWrapper不起作用,我认为Map也可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM