0

I developed an Angular application and I will consume the RESTful service developed using Java (JAX-RS - Jersey).

This is my service:

@POST
@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addNewContact(Contact instance) {
    Contact contact = new Contact(instance.getLastName(), instance.getFirstName(), instance.getPhone_1(),
            instance.getPhone_2(), instance.getEmail());
    contact.setPicture("Unknown.png");
    try {
        dao.save(contact);
        return Response.status(200).entity(contact).build();
    } catch (Exception e) {
        return Response.status(400).entity("IMPOSSIBLE D'AJOUTER CETTE PERSONNE").build();
    }
}

This is the config in web.xml :

<servlet>
 <servlet-name>jersey-serlvet</servlet-name>
 <servlet-class>
    com.sun.jersey.spi.container.servlet.ServletContainer
 </servlet-class>
 <init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>ma.restfull.services</param-value>
 </init-param>
 <init-param>
  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

My entity is this:

@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "contact")
public class Contact {

@Id
@GeneratedValue
private int id;

@Column(name = "first_name", length = 50, nullable = false, unique = false, updatable = true, insertable = true)
@Getter
@Setter
private String firstName;

@Column(name = "last_name", length = 50, nullable = false, unique = false, updatable = true, insertable = true)
@Getter
@Setter
private String lastName;

@Column(name = "phone_1", nullable = true, unique = false, updatable = true, insertable = true)
@Getter
@Setter
private String phone_1;

@Column(name = "phone_2", nullable = true, unique = false, updatable = true, insertable = true)
@Getter
@Setter
private String phone_2;

@Column(name = "email", nullable = true, unique = true, updatable = true, insertable = true)
@Getter
@Setter
private String email;

@Column(name = "picture", nullable = true, unique = false, updatable = true, insertable = true)
@Getter
@Setter
private String picture;

public Contact(String lastName, String firstName, String phone_1, String phone_2, String email) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.phone_1 = phone_1;
    this.phone_2 = phone_2;
    this.email = email;
}
}

but, when I send the request I get this Error in my console

the object sent by Angular is like this:

Object { lastName: "Nom", firstName: "Prénom", phone_1: "12346565464", phone_2: "1234567890", email: "aaa@aaa.com", id: 0, picture: "Unknown.png" } 
jasonlam604
  • 1,456
  • 2
  • 16
  • 25
Mahmoud
  • 325
  • 10
  • 25
  • `public class Contact { private int id; ... private String picture; ... }` Those are fields in the `Contact` class but are not present in your json sent to the server, so they cannot be mapped to a proper `Contact` instance. – Luiggi Mendoza Feb 09 '16 at 15:02
  • I send this object know, but the same error : { lastName: "Nom", firstName: "Prénom", phone_1: "12346565464", phone_2: "1234567890", email: "aaa@aaa.com", id: 0, picture: "Unknown.png" } – Mahmoud Feb 09 '16 at 15:17
  • Response is bad `Response.status(400).entity("IMPOSSIBLE D'AJOUTER CETTE PERSONNE").build();` You need to return object, not string as you've declared Produces JSON object – Valijon Feb 09 '16 at 15:22
  • @LuiggiMendoza I Edit my Question . Valijon I can not run the method , I do not get this line ; – Mahmoud Feb 09 '16 at 15:25
  • Silly me. Add a public constructor with no arguments, something like `public Contact() { }`. – Luiggi Mendoza Feb 09 '16 at 16:50
  • 1
    @LuiggiMendoza : I find the problem. In pom.xml I forgotthe dependency of jakson: jackson-mapper-asl AND jersey-bundle. when I remove the both dependency I have another error : The ResourceConfig instance does not contain any root resource classes. I resolved them by removing the / in Path annotation of my service LuiggiMendoza : thank you for your quickly response – Mahmoud Feb 09 '16 at 16:50
  • I have flagged this as "Off-topic due to typographical error", especially since the answer is from the OP and buried in the comments. The typographical error is due to the fix the OP mentioned. – krillgar Feb 09 '16 at 20:24

0 Answers0