Java JAX-RS,JAXB作为字符串xml文件返回

Java JAX-RS,JAXB作为字符串xml文件返回,java,jaxb,jax-rs,Java,Jaxb,Jax Rs,我遇到以下错误: 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

我遇到以下错误:

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";
    }
@服务
@路径(“/services/”)
公共类UserServiceImpl实现UserService{
List validUsers=Arrays.asList(“test”、“admin”);//测试目的
List validPassword=Arrays.asList(“test1”、“admin1”);
字符串USER_DETAILS_XML=“./USER DETAILS.XML”;
字符串USER\u ERROR\u XML=“./USER ERROR.XML”;
/**
*此方法基于临时用户id和密码验证用户登录凭据,并返回消息
*@param用户名
*@param密码
*@返回用户消息
*/
@得到
@路径(“{userName}/{password}”)
@生成(MediaType.TEXT\u XML)
公共字符串登录(@PathParam(“用户名”)字符串用户名,@PathParam(“密码”)字符串密码)
抛出JAXBEException、PropertyException、FileNotFoundException{
用户=新用户();
InvalidUser InvalidUser=新的InvalidUser();
if((validUsers.contains(用户名)和&validPassword.contains(密码))){
user.setUserName(用户名);
user.setFirstName(getName());
user.setLastName(“Tom”);
返回validUser(用户);
}
否则{
无效用户设置码(400);
invalidUser.setMessage(“无效凭据”);
返回invalidUser(invalidUser);
}
}
/*
*此方法验证并创建用户详细信息xml文件
*/
私有字符串validUser(用户用户)抛出JAXBEException、PropertyException、FileNotFoundException{
JAXBContext JAXBContext=JAXBContext.newInstance(User.class);
Marshaller=jaxbContext.createMarshaller();
setProperty(marshaller.JAXB_格式的_输出,Boolean.TRUE);
marshaller.marshall(用户,新文件(用户详细信息XML));
返回“成功”;
}
/*
*此方法验证并创建用户错误xml文件
*/
私有字符串invalidUser(invalidUser invalidUser)引发JAXBEException、PropertyException、FileNotFoundException{
JAXBContext JAXBContext=JAXBContext.newInstance(InvalidUser.class);
Marshaller=jaxbContext.createMarshaller();
setProperty(marshaller.JAXB_格式的_输出,Boolean.TRUE);
marshaller.marshall(invalidUser,新文件(USER\u ERROR\u XML));
返回“失败”;
}
我想以XML字符串的形式返回。如何将jaxb转换/返回为XML字符串文件?

jaxb

@XmlRootElement
公共类用户{
}
JAX-RS将为您做这件事

@服务
@路径(“/services”)
公共类UserServiceImpl实现UserService{
@得到
@路径(“{userName}/{password}”)
@生成(MediaType.APPLICATION\u XML)
公共用户登录(@PathParam(“用户名”)字符串用户名,
@PathParam(“密码”)字符串(密码)
抛出JAXBEException、PropertyException、FileNotFoundException{
最终用户=findUser();
/**
//甚至不是必需的;JAX-RS会说404代表null
if(user==null){
返回Response.status(Response.status.NOT_FOUND).build();
}
**/
返回用户;
}
}
这将转换为字符串格式。感谢所有给予支持的人

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();
    }