Angular, Java Service REST

Asked

Viewed 409 times

1

I am trying to make persistence (insert data) using Angularjs and Webservice REST in Java, when I click Insert in the console, the following error appears:

POST http://localhost:8080/Cast_fleets/Rest/course/Insert 415 (Unsupported Media Type).

What could be?

//WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         id="WebApp_ID" 
         version="2.5">

  <!-- COMO DEFAULT, O NOME DO MEU PROJETO, ESSE NOME IRÁ APARECER NA URL -->
  <display-name>Cast_Frotas</display-name> 

  <!-- MAPEANDO O SERVLET-->
  <servlet> 
    <servlet-name>Jersey RESTfull</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>br.com.Cast_frotas.service</param-value>
    </init-param> 

    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <servlet-name>Jersey RESTfull</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

//MY CONTROL IN ANGULAR

$scope.inserir = function (){
    $http.post(linkservice + "insert", $scope.curso).then(function(data){
        alert('Curso inserido com sucesso!');
    });
}

//COURSE CLASS

public class Curso {
    private int id;
    private String nome;
    private String descricao;

    public Curso() {

    }

    public int getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
}

//CLASS CTRLCURSO

public class CtrlCurso {

    public boolean insert(Curso c){ 
        return CursoDB.getInstance().insert(c); 
    }

    public List<Curso> getList(){   
        return CursoDB.getInstance().getListAll();  
    }
}

//MY SERVICE

@POST
@Path("/insert")
public void inserirCurso(Curso c){  
    new CtrlCurso().insert(c);
}
  • What library are you using to process JSON on your Java Webservice?

  • Felipe, I’m using Jsonobject

1 answer

1

You need to add the annotation @Consumes(MediaType.APPLICATION_JSON):

@POST
@Path("/insert")
@Consumes(MediaType.APPLICATION_JSON)
public void inserirCurso(Curso c){  
    new CtrlCurso().insert(c);
}
  • Felipe, thank you for answering, once again. I am able to insert, however, the object comes null to my service, ie insert null Tuno. I don’t need to access json values and put in variables?

  • my code: @POST @Path("/Insert") @Consumes(Mediatype.APPLICATION_JSON) public void insert Stroke c = new Stroke(); System.out.println(c.getDescription(); Try{ new Ctrlcurso(). Insert(c); }catch(Exception e){ e. printStackTrace(); } }

  • @Matheusminguini Why did you take the parameter Curso method? Is this parameter Curso which you will receive in the Angular request. What you did in the above code was to create a new instance of the object Curso and not assign any value to its properties. When you say "null inserts" it would be about your getListAll return nothing?

  • Returns yes, returns a normal JSON! Actually, I didn’t notice that Via took the function receiving parameter, but after placing the parameter, error 415 (Unssuported Mediatype) returned. But yes, my getListAll() function takes the database data and returns it in JSON format

  • @Matheusminguini Edit your question and ask yours pom.xml, web.xml and its classes CtrlCurso, Curso and `Cursoservice, please.

  • edited. I don’t own pom.xml, this file is for projects with Maven, right?

  • @Matheusminguini Yes, it is. Are you adding all the dependencies on hand? What courage! Could you list them for me to test here? Also, put the class CursoService complete please.

  • Feipe, I got it here! But thank you so much, you’ve been helping me all day! I was trying to receive a course object, when I should actually receive a String, pass it to JSON and then build an object from the JSON attributes. Thank you very, very successful for you! How do I post the solved code?

  • @Matheusminguini Since you have less than 100 reputation, you need to wait 8 hours to answer your question. Also, to mark it as correct, you need to wait 48 hours. fountain. As for receiving as a String, This is not necessary. Jersey can convert JSON to an object. You just need to apply a small configuration (which varies according to the JSON processor you are using) to make it for you. Take a look at that link.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.