0

I can"t understand the problem because i found nowhere the same problem. i tried to insert product with type "pièces détachées". On the browser path /Produits the product doesn't show up but when i remove the accents from the string it's inserting in the mysql db. I tried to insert accents in the db the query is ok. But with my code it's impossible to do the same. here's a sample of my code for Produit :

 //ajouter un produit
@PostMapping(value = "/Produits")
public ResponseEntity<Void> ajouterProduit(@RequestBody ProduitDTO produit) {
    Produit produitCK = new Produit();
    produitCK.setIdPM(new ProduitMagasin(produit.getId(), produit.getIdMagasin()));
    produitCK.setDateAjout(produit.getDateAjout());
    produitCK.setEtatProduit(produit.getEtatProduit());
    produitCK.setIdReference(produit.getIdReference());
    produitCK.setNumeroSerie(produit.getNumeroSerie());
    produitCK.setPrixVente(produit.getPrixVente());
    produitCK.setTVA(produit.getTVA());
    produitCK.setType(produit.getType());
    Produit produitAdded =  produitDao.save(produitCK);
    if (produitAdded == null)
        return ResponseEntity.noContent().build();
    else
        return new ResponseEntity<>(HttpStatus.CREATED);
}

ProduitDTO :

package com.stationphone.db.model;

import java.sql.Timestamp;

    /**
     *
     * @author Administrateur
 */
public class ProduitDTO {
     private int id;

    public enum Type {
        PIECES {
          public String getString() {
              return "pièces detachées";
          }
         },    
         VENTE {
      public String getString() {
          return "vente en magasin";
      }
    };

    public abstract String getString();
}

private String type;

private int id_reference;

private String numero_serie;

private Timestamp date_ajout;

private String marque;

private String modele;

private float prix_vente;
private double prixHT;
private float tva;

private String etat_produit;
private int id_magasin;

public int getId(){
   return this.id;
}

public void setId(int id){
    this.id=id;
}

public int getIdOnline(){
   return this.id;
}

public void setIdOnline(int id){
    this.id=id;
}

public String getType(){
    return this.type;
}

public void setType(String type){
    this.type=type;
}

public int getIdReference(){
    return this.id_reference;
}

public void setIdReference(int id_reference){
    this.id_reference=id_reference;
}

public String getNumeroSerie(){
    return this.numero_serie;
}

public void setNumeroSerie(String numero_serie){
    this.numero_serie=numero_serie;
}

public Timestamp getDateAjout(){
    return this.date_ajout;
}

public void setDateAjout(Timestamp date_ajout){
    this.date_ajout=date_ajout;
}

public String getMarque(){
    return this.marque;
}

public void setMarque(String marque){
    this.marque=marque;
}

public String getModele(){
    return this.modele;
}

public void setModele(String modele){
    this.modele=modele;
}

public float getPrixVente(){
    return this.prix_vente;
}

public void setPrixVente(float prix){
    this.prix_vente=prix;
}

public double getPrixHT(){
    return this.prixHT;
}

public void setPrixHT(double prix){
    this.prixHT=prix;
}

public float getTVA(){
    return this.tva;
}

public void setTVA(float tva){
    this.tva=tva;
}

public String getEtatProduit(){
    return this.etat_produit;
}

public void setEtatProduit(String etat){
    this.etat_produit=etat;
}

public int getIdMagasin(){
    return this.id_magasin;
}

public void setIdMagasin(int id_magasin){
    this.id_magasin=id_magasin;
}

}

class Produit :

    package com.stationphone.db.model;

import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

/**
 *
 * @author Administrateur
 */
@Entity
public class Produit implements Serializable {

    @EmbeddedId 
    private ProduitMagasin idPM;

    @OneToOne
@JoinColumn(name = "id_magasin", insertable=false, updatable=false)
Magasin magasin;

public enum Type {
    PIECES {
      public String getString() {
          return "pièces detachées";
      }
     },    
     VENTE {
      public String getString() {
          return "vente en magasin";
      }
    };

    public abstract String getString();
}

private String type;

private int id_reference;

private String numero_serie;

private Timestamp date_ajout;

private float prix_vente;
private float tva;

private String etat_produit;

public ProduitMagasin getIdPM(){
   return this.idPM;
}

public void setIdPM(ProduitMagasin id){
    this.idPM=id;
}

public String getType(){
    return this.type;
}

public void setType(String type){
    this.type=type;
}

public int getIdReference(){
    return this.id_reference;
}

public void setIdReference(int id_reference){
    this.id_reference=id_reference;
}

public String getNumeroSerie(){
    return this.numero_serie;
}

public void setNumeroSerie(String numero_serie){
    this.numero_serie=numero_serie;
}

public Timestamp getDateAjout(){
    return this.date_ajout;
}

public void setDateAjout(Timestamp date_ajout){
    this.date_ajout=date_ajout;
}


public float getPrixVente(){
    return this.prix_vente;
}

public void setPrixVente(float prix){
    this.prix_vente=prix;
}


public float getTVA(){
    return this.tva;
}

public void setTVA(float tva){
    this.tva=tva;
}

public String getEtatProduit(){
    return this.etat_produit;
}

public void setEtatProduit(String etat){
    this.etat_produit=etat;
}

}

I hope i'll find an answer here. Thanks.

EDIT : the post method :

public static HttpResponse post(Object object, String type){
    HttpResponse response=null;
    try{
    CloseableHttpClient client = HttpClientBuilder.create().build();  
    HttpPost postRequest = new HttpPost(getUrl(type));
    System.out.println(getUrl(type));
    ObjectMapper mapper= new ObjectMapper();
    String jsonInString = mapper.writeValueAsString(object);
    System.out.println(jsonInString);
    StringEntity input = new StringEntity(jsonInString);
    input.setContentType("application/json");
    postRequest.setEntity(input);

    response = client.execute(postRequest);

    if (response.getStatusLine().getStatusCode() != 201) {
        throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatusLine().getStatusCode());

    }

    BufferedReader br = new BufferedReader(
                    new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
            System.out.println(response.getStatusLine().getStatusCode()+" ");

            client.close();
      } catch (ClientProtocolException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();
  } finally{
        return response;
      }

EDIT 2 : the error on tomcat local :

018-07-24 18:55:32.196 WARN 10860 --- [nio-8084-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid UTF-8 middle byte 0x63

at [Source: (PushbackInputStream); line: 1, column: 21]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x63

at [Source: (PushbackInputStream); line: 1, column: 21]

at [Source: (PushbackInputStream); line: 1, column: 16] (through reference

chain: com.stationphone.db.model.ProduitDTO["type"])

cilies38
  • 69
  • 1
  • 8
  • I wonder what exception you get when you have accents for a call to `persist` ... without telling people you don't give them a chance of answering –  Jul 24 '18 at 12:50
  • i didn't see any exception and the http response is 201 (created). – cilies38 Jul 24 '18 at 12:57
  • i just noticed it doesn't print the http response at all when there is an accent when it's different from 201 it raises an exception but it doesn't display anything. Normally it should display code 201 or execption. – cilies38 Jul 24 '18 at 13:12
  • Anything that "doesn't work" will give an error ... either returned from a call, or in a log. Without such basic info there's nothing to say –  Jul 24 '18 at 13:58
  • i forgot the web service is server side i thought the modifications i made would be ok i'll test the program locally – cilies38 Jul 24 '18 at 16:33
  • so the accent cause a json parse error view edit 2. – cilies38 Jul 24 '18 at 16:59
  • So the problem is nothing to do with the JPA API, and is simply JSON/Jackson. Searching on this site reveals https://stackoverflow.com/questions/22457525/jackson-mapper-with-special-character-%C3%A9-issue –  Jul 24 '18 at 17:02
  • Possible duplicate of [jackson JsonParseException: Invalid UTF-8 middle byte](https://stackoverflow.com/questions/6352861/jackson-jsonparseexception-invalid-utf-8-middle-byte) – Raedwald Aug 23 '18 at 08:12

1 Answers1

0

Thanks tp billy frost what to do 2 things :

HttpPost postRequest = new HttpPost(getUrl(type));
        postRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity input = new StringEntity(jsonInString, "UTF-8");
cilies38
  • 69
  • 1
  • 8