0

Hi all I want to return a userName with a rest http request in Spring + Mysql.

Controller:

@Controller
@SessionAttributes("user")
public class UserController {

    @Autowired
    private UserService userService;

     @RequestMapping(method=RequestMethod.GET, value="/userss/{userName}")
     public @ResponseBody String getUserName(@PathVariable String userName) {
        return userName;
     }
}

Model:

@Entity
@Table(name="users")
public class User {

    @Id
    @GeneratedValue
    private Long id;

    @NotEmpty
    @Size(min=4, max=20)
    private String userName;

    @NotEmpty
    private String firstName;

    @NotEmpty
    private String lastName;

    @NotEmpty
    @Size(min=4, max=8)
    private String password;

    @NotEmpty
    private String status;


    @NotEmpty
    @Email
    private String emailAddress;


    @NotNull
    @Past
    @DateTimeFormat(pattern="MM/dd/yyyy")
    private Date dateOfBirth;

    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }



    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }   
}

I just want to retrieve my services put,delete,post and get with requestmappings connecting with my model to my mysql DB. I´m trying to make the request in this way: http://localhost:8080/portuzona/userss?userName=Arnoldo

But I get this:

HTTP Status 404 - /portuzona/userss

type Status report

message /portuzona/userss

description The requested resource is not available.

portuzona and userss is correctly handled (html views) but I cannot make a get in my controllers.

Log says correctly the Table has been found by the bean:

sep 25, 2015 2:07:22 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: portuzona.users
sep 25, 2015 2:07:22 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [dateofbirth, password, firstname, emailaddress, id, username, lastname, status]

As a User requested here is more info that could be help .. or not:

/WEB-INF/servlet.xml

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

<servlet>
    <servlet-name>usersHibernateServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servletConfig.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>usersHibernateServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/jpaContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <display-name>Archetype Created Web Application</display-name>
</web-app>

/WEB-INF/config/servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<mvc:annotation-driven />

<context:component-scan base-package="com.portuzona" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
</bean>

</beans>

2 Answers 2

1

Looks like you're mixing up mapping and path variables. Two options:

1:

@RequestMapping(method=RequestMethod.GET, value="/userss/{userName}")
public @ResponseBody String getUname(@PathVariable String userName) {
   return userName;
}

http://localhost:8080/portuzona/userss/Arnoldo

2:

@RequestMapping(method=RequestMethod.GET, value="/userss/yourmapping")
public @ResponseBody String getUserName(@RequestParam String username) {
    return username;
}

http://localhost:8080/portuzona/userss/yourmapping?username=Arnoldo

Sign up to request clarification or add additional context in comments.

7 Comments

Hi man, both ways brings me: type Status report message /portuzona/userss/yourmapping description The requested resource is not available.
Fixed my original post, sorry for the delay. Tested those options and either works fine.
No man, haha no luck here. Same result with both things... I think maybe is something about configuration... not sure... Request URL:localhost:8080/portuzona/userss?userName=Arnoldo Request Method:GET Status Code:404 Not Found
Can't help you without further information (e.g. portuzona mapping), please check it and post further code if necessary.
Where could be that? :O Pardon my ignorance
|
0

Try the following with http://localhost:8080/portuzona/userss?userName=Arnoldo

@RequestMapping(method=RequestMethod.GET, value="/userss")
public @ResponseBody String getUserName(@RequestParam String userName) {
   return userName;
}

1 Comment

Didnt work .... type Status report message /portuzona/userss description The requested resource is not available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.