0

我收到以下错误:

XML Parsing Error: syntax error
Location: http://localhost:8080/assignment/services/services/test/test1
Line Number 1, Column 1:sucess
^

我的Java代码是:

@Service
@Path("/services/")
public class UserServiceImpl implements UserService {

    List<String> validUsers = Arrays.asList("test", "admin"); // testing purpose
    List<String> validPassword = Arrays.asList("test1", "admin1");
    String USER_DETAILS_XML = "./user-details.xml";
    String USER_ERROR_XML = "./user-error.xml";

    /**
     * This method validates user login credentials based on temporary user id and password and returns the message 
     * @param username
     * @param password
     * @return user message
     */
    @GET
    @Path("{userName}/{password}")
    @Produces(MediaType.TEXT_XML)
    public String login(@PathParam("userName")String username, @PathParam("password")String password)
            throws JAXBException, PropertyException, FileNotFoundException {
        User user = new User();
        InvalidUser invalidUser = new InvalidUser();
        if ((validUsers.contains(username) && validPassword.contains(password))) {
            user.setUserName(username);
            user.setFirstName(getName());
            user.setLastName("Tom");
            return validUser(user);
        }
        else{
            invalidUser.setCode(400);
            invalidUser.setMessage("Invalid Credentials");
            return invalidUser(invalidUser);
        }
    }

    /*
     * This method verifies and creates user details xml file
     */

    private String validUser(User user) throws JAXBException, PropertyException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(user,new File(USER_DETAILS_XML));
        return "sucess";
    }
    /*
     * This method verifies and creates user error xml file
     */
    private String invalidUser(InvalidUser invalidUser) throws JAXBException,PropertyException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(InvalidUser.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(invalidUser, new File(USER_ERROR_XML));
        return "failure";
    }

我想以 XML 字符串的形式返回。如何将 jaxb 转换/返回为 xml 字符串文件?

4

2 回答 2

2
private String validUser(User user) throws JAXBException, PropertyException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(user,new File(USER_DETAILS_XML));
        marshaller.marshal(user,stringWriter);
        return stringWriter.toString();
    }
    /*
     * This method verifies and creates user error xml file
     */
    private String invalidUser(InvalidUser invalidUser) throws JAXBException,PropertyException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(InvalidUser.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(invalidUser, new File(USER_ERROR_XML));
        marshaller.marshal(invalidUser, stringWriter);
        return stringWriter.toString();
    }

这将转换为字符串格式。感谢每一位给予支持的人。

于 2013-07-30T02:28:40.650 回答
0

JAXB

@XmlRootElement
public class User {
}

JAX-RS 将为您完成。

@Service
@Path("/services")
public class UserServiceImpl implements UserService {

    @GET
    @Path("{userName}/{password}")
    @Produces(MediaType.APPLICATION_XML)
    public User login(@PathParam("userName")String username,
                      @PathParam("password")String password)
        throws JAXBException, PropertyException, FileNotFoundException {

        final User user = findUser();

        /**
        // not even required; JAX-RS will say 404 for null
        if (user == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        **/

        return user;
    }
}
于 2013-07-30T02:23:53.767 回答