When registering data in my database using webservice the following error occurs.
Sending 'POST' request to URL : http://localhost:8080/FazendaWS/webresources/fazenda/Usuario/inserir
Post parameters : {"login":"felipe10","senha":"1234","email":"[email protected]","perfil":"teste100"}
Response Code : 415
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 415 for URL: http://localhost:8080/FazendaWS/webresources/fazenda/Usuario/inserir
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at consumindows.HttpExemplo.sendPost(HttpExemplo.java:165)
at consumindows.HttpExemplo.main(HttpExemplo.java:50)
Caused by: java.io.IOException: Server returned HTTP response code: 415 for URL: http://localhost:8080/FazendaWS/webresources/fazenda/Usuario/inserir
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at consumindows.HttpExemplo.sendPost(HttpExemplo.java:159)
... 1 more
REST Web Service
package ws;
import com.google.gson.Gson;
import dao.UsuarioDAO;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import modelo.Usuario;
/**
* REST Web Service
*
* @author Felipe
*/
@Path("fazenda")
public class FazendaWS {
@Context
private UriInfo context;
public FazendaWS() {
@POST
@Consumes({"application/json"})
@Path("Usuario/inserir")
public boolean inserir(String content){
Gson g = new Gson();
Usuario u = (Usuario) g.fromJson(content, Usuario.class);
UsuarioDAO dao = new UsuarioDAO();
return dao.inserir(u);
}
}
}
link
package consumindows;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy.Type;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import modelo.Usuario;
public class HttpExemplo {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpExemplo http = new HttpExemplo();
Gson g =new Gson();
Usuario u = new Usuario();
java.lang.reflect.Type usuarioType= new TypeToken<Usuario>() {}.getType();
String url;
u.setEmail("[email protected]");
u.setLogin("felipe10");
u.setPerfil("teste100");
u.setSenha("1234");
System.out.println(u.getLogin());
String json=g.toJson(u, usuarioType);
url="http://localhost:8080/FazendaWS/webresources/fazenda/Usuario/inserir";
http.sendPost(url,json,"POST");
}
// HTTP POST request
private void sendPost(String url,String urlParameters,String method) throws Exception {
URL obj= new URL(url);
HttpURLConnection con=(HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod(method);
con.setRequestProperty("User-Agent", "application/json");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}