Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
Having issues i cant seem to hit my controller, please help

Web.xml below.

XML
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>mvc_1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>

  </welcome-file-list>
  <servlet>
        <servlet-name>Jersey</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>org.user</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>


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

</web-app>




JavaScript
var sUsername = $("#tbUsername").val();
	var sPassword = $("#tbPassword").val();
	var jsonObj = {"username":sUsername,"password":sPassword};

$.ajax
	({
		url:"http://localhost:8080/mvc_1/rest/controller/doPosting",
		type:'POST',
		dataType:'json',
		data:JSON.stringify(jsonObj),
        contentType: "application/json; charset=utf-8",
		success: function(data)
		{
			alert("success!!")
		},
		error:function(XHR,status,er)
		{
			alert(XHR.status +" : "+er+" : "+status);
		}
	}); 


Controller below:

Java
@Path("/controller")
public class UserController {
	
	@POST
	@Path("/doPosting")
	@Consumes({ MediaType.APPLICATION_JSON })
	public String doPosting(User user) {

		String output = user.getUsername();
		System.out.println(output);

		String json = new Gson().toJson(user);

		System.out.println("Hi");

		return json;

	}
}
Posted
Comments
[no name] 24-Sep-14 3:53am    
when are you getting this error
Anele Ngqandu 24-Sep-14 3:59am    
after ajax post goes to the error function
[no name] 24-Sep-14 4:04am    
write [HttpPost] instead of @POST
@Path("/doPosting")
@Consumes({ MediaType.APPLICATION_JSON })

1 solution

For 500 Internal server error, you ought to debug and check for the Issue on the server side code. Please debug putting the code block in try catch and then check for exception.
Thanks
Please reply after debug. :)
 
Share this answer
 
Comments
Anele Ngqandu 24-Sep-14 4:25am    
"the resource is not available" that am assuming its talking about the url on ajax
[no name] 24-Sep-14 4:39am    
I guess yes. Please check that and resolve asap.. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900