它是我在Java中的第一个API,所以请记住这一点;)我已经掌握了Controller.class,我现在保留了所有方法(我稍后会清理它) ;))。就GET方法完美运行而言,当我想创建新用户时,我遇到了PUT方法的问题 - 它不起作用。有什么帮助吗?并且建议原因 - 正如我所说的 - 我是API的新手;)
所以这是我的controller.class:
invoiceid
正如您所看到的,最后一种方法是PUT并且它不起作用:这是我的测试客户端:
@Path("/api")
public class Controller {
List<Post> mBartoszPosts;
List<Post> mFelipePosts;
List<Post> mShawnPosts;
List<Post> mDavidPosts;
List<Post> mGraziellaPosts;
List<Post> mAllPosts;
List<User> mUsers;
User bartosz;
User felipe;
User shawn;
User david;
User graziella;
@Path("/user/{id}")
@GET
@Produces("application/json")
public Response getUser(@PathParam("id")int id) {
setUpUsers();
System.out.println("Liczba osob : " + mUsers.size());
for(User user : mUsers) {
if(id == user.getId()) {
String result = user.toString();
return Response.status(200).entity(user).build();
}
}
return null;
}
@Path("/post/{post_id}")
@GET
@Produces("application/json")
public Response getPost(@PathParam("post_id")int post_id) {
setUpUsers();
System.out.println("Liczba osob : " + mUsers.size());
for(Post post : mAllPosts) {
if(post_id == post.getId()) {
String result = post.toString();
return Response.status(200).entity(post).build();
}
}
return null;
}
@Path("/posts")
@GET
@Produces("application/json")
public Response getPosts() {
setUpUsers();
String response = new Gson().toJson(mAllPosts );
return Response.status(200).entity(response).build();
}
@PUT
@Path("user/new/{id}/{post}")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response updateEmployeeById(@PathParam("id") Integer id,@PathParam("post") String userPost)
{
List<Post>userPosts = new ArrayList();
Post post = new Post(99,userPost,"Bartosz");
userPosts.add(post);
User updatedEmployee = new User(id,"Bartek","Szlapa",userPosts);
if(updatedEmployee.getName() == null) {
return Response.status(400).entity("Please provide the employee name !!").build();
}
updatedEmployee.setId(id);
updatedEmployee.setName(updatedEmployee.getName());
System.out.println(updatedEmployee.getName());
return Response.ok().entity(updatedEmployee).build();
}
public int maxValue(int array[]){
int max = Arrays.stream(array).max().getAsInt();
return max;
}
}
最后我的错误:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Client client = ClientBuilder.newClient( new ClientConfig().register( Controller.class ) );
WebTarget webTarget = client.target("http://localhost:8080/JerseyDemos/rest").path("api").path("user").path("new").path("77");
List<Post>userPosts = new ArrayList();
Post post = new Post(99,"Bartek Szlapa","Testing post ...");
userPosts.add(post);
User emp = new User(99,"Bartek","Szlapa",userPosts);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML);
Response response = invocationBuilder.put(Entity.entity(emp, MediaType.APPLICATION_XML));
User user = response.readEntity(User.class);
System.out.println(response.getStatus());
System.out.println(user);
}
}
提前感谢您的帮助!
答案 0 :(得分:1)
您的客户端代码中似乎存在2个问题 -
要删除此错误,您应在类路径中添加-118
[menu]
。如果您使用的是maven,请在content/
-
example.com/about
根据您的jersey-media-jaxb
方法API规范 -
JAR
您的REST服务仅需pom.xml
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>${jersey.version}</version>
</dependency>
和PUT
。无需其他输入。但是,在您的客户端代码中,您在调用API时发送public Response updateEmployeeById(@PathParam("id") Integer id,@PathParam("post") String userPost) {
类对象path parameter
对象 -
id
由于您未在API中收到此post
对象,因此此数据将丢失。因此,您应该更新API以接受请求中的User
类对象,或者不要从客户端代码中发送此对象。