问题描述:

I am still working on a jax-rs server, and I faced some new problems recently. I do not understand where I define the name of my webserver. I searched everything in my workspace, but couldn't find anything.

Let's roll out the problem a bit further:

I always reach my server's @GET method via http://XXXXXX.XXXXX.XXX-XXXXXXX.de/android/

This is the structure of my server class:

@Path("/users")

public class UserResource {

Connection dbconn = null;

public UserResource() {

userIds = new ArrayList<Integer>();

userIds.add(1);

userIds.add(2);

userIds.add(3);

}

@GET

@Path("/login/{id}")

@Consumes("application/xml")

public StreamingOutput getTests(@PathParam("id") int id, InputStream is) {

return new StreamingOutput() {

public void write(OutputStream outputStream) throws IOException,

WebApplicationException {

getTests(outputStream);

}

};

}

As you see, the path of my class is "/users", and the path of the @GET method is "/login/1" (for example id = 1). Now I tried to call the method via

http://XXXXXX.XXXXX.XXX-XXXXXXX.de/android/users/login/1

But this does not work. I get an error (unknown source). And my error.log says that it couldn't find the resource at

http://XXXXXX.XXXXX.XXX-XXXXXXX.de/users/users/login/1

My 1st question: Where does the double "/users" come from? I have no idea. When I leave away the "/users" in my request url, there will be only 1 "/users" in the error.log, but still the resource is not found.

And there is another thing I did not find out yet: How do I change the name of my service? Atm, it's "android", but how could I change this? I searched my whole workspace, found "android" in my pom.xml, but when i change it to, let's say "testandroid", upload everything, build and run the server, the name is still android. No idea why this is the case.

Thx for your help guys!

EDIT:

This is my "main" class:

package com.restfully.services;

import javax.ws.rs.core.Application;

import java.util.HashSet;

import java.util.Set;

public class ServerApplication extends Application {

private Set<Object> singletons = new HashSet<Object>();

private Set<Class<?>> empty = new HashSet<Class<?>>();

public ServerApplication() {

singletons.add(new UserResource());

}

@Override

public Set<Class<?>> getClasses() {

return empty;

}

@Override

public Set<Object> getSingletons() {

return singletons;

}

}

I am using Eclipse and Maven. The application runs on a jetty-server. If you could use any further information, let me know.

网友答案:

You can look in the following places

Pom.xml file for context root the following entry;

<configuration>
    <context>yourWarName</context>
</configuration>

Using Netbeans check Run Category context path under project properties.

Context Path: /yourWarName  

Have a look in your web.xml as well.

When using jax-rs you normally define a config class;

@ApplicationPath("resources")
public class RestConfig extends Application{

}

From there you define your other paths;

    @Stateless
    @Path("/login")
    public class LoginResource

    public Response login(Credentials credentials) {
    Credentials result = this.loginService.login(credentials);
            return Response.status(Response.Status.OK).entity(result).build();
     }

The path to the following is: http://domain.com/MyApp/resources/login where MyApp is the context root.

It might be that there is a path specified in config or root with the name users that you are getting the double users.

相关阅读:
Top