0

This is my REST api for different operation.

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService") 

public class UserService {  
   UserDao userDao = new UserDao();  
   @GET 
   @Path("/users") 
   @Produces(MediaType.APPLICATION_XML) 
   public List<User> getUsers(){ 
      return userDao.getAllUsers(); 
   }  

   @POST
   @Path("/post")
   @Produces(MediaType.APPLICATION_XML) 
   public String setUsers(){
       return "Hello";
   }

   @POST
   @Path("/newentry")
   @Produces(MediaType.APPLICATION_XML) 
   @Consumes(MediaType.APPLICATION_XML)
   public List<User> newEntry(User user,String name){
       List<User> newUser = new ArrayList<User>();
       User newuser = new User(user.getId(),name,user.getProfession());
       newUser.add(newuser);
       return newUser;
   }

}

But passing the REST request as below throws error Response as 500

<user><id>1</id><name>Foo</name><profession>SE</profession></user><name>bar</name>

Please guide , how to send the request or how we can handle multiple argument

deva
  • 49
  • 6
  • I am assuming you are using JAXB to bind your User Entity. If yes then make sure your User.java has this annotation `@XmlRootElement(name = "user")` – piy26 Jun 27 '18 at 12:26
  • @piy26 Yes I am using JAXB to bind the entity , but the user.java already have '@XmlRootElement' annotation – deva Jun 27 '18 at 12:35
  • Can you copy the stacktrace from the server logs. One thing that I can guess is you are not passing correct `headers` i.e. `Accept: and Content-Type:` headers along with your request – piy26 Jun 27 '18 at 12:39
  • Have you consider using the path variable ? It would work in your case since you are asking for a String. replace `@Path("/newentry")` by `@Path("/newentry/{name}")` and add `@PathParam("name")` before String name in method signature. I don't understand why you need the name aside though. Doesnt look like a best practice – gnos Jun 27 '18 at 14:22

0 Answers0